在花费大量时间重新安装,检查工作并从头开始之后,我仍然没有按照MyFirstApp Android教程解决问题。我正在使用Android eclipse捆绑软件对其进行编码,因此支持包应全部正确安装。我的错误发生在以下代码中(错误旁边注释了来自编译器的错误)

package com.example.myfirstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
        /** Called when the user clicks the Send button */
        /** Called when the user clicks the Send button */
        public void sendMessage(View view) {
            Intent intent = new Intent(this, DisplayMessageActivity.class); // **Error"The constructor Intent(MainActivity.PlaceholderFragment, Class<DisplayMessageActivity>) is undefined"**
            EditText editText = (EditText) findViewById(R.id.edit_message);// **Error "edit_message cannot be resolved or is not a field"**
            String message = editText.getText().toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
        }

    }

}


任何帮助将不胜感激。据我所知,本教程中的所有说明均已正确复制。

最佳答案

对于第一个错误,您只需将this替换为getActivity(),因为this引用您的PlaceholderFragment实例,该实例不是有效的Context,但包含的Activity是。

Intent intent = new Intent(this, DisplayMessageActivity.class);


改成

Intent intent = new Intent(getActivity(), DisplayMessageActivity.class);


您的第二个错误可能意味着您的布局中没有定义id edit_message,或者这意味着您只是忘记了导入R类。

话虽如此:如果您必须在这里询问这些错误,我强烈建议您在深入研究Android开发之前先读一本Java书籍。

关于android - MyFirstApp Android Coding教程中的错误“意向构造函数未定义。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23223916/

10-11 22:21
查看更多