意图未定义尝试了很多,没有任何作用

时间:2014-03-22 22:42:34

标签: java android eclipse android-intent undefined

我在Intent中遇到错误。它说:intent is undefined。我一直在搜索很多其他帖子,但它仍然无效。有什么想法吗?

根据您的知识,我开发了自己的应用程序与android developpes的基础。因为我自己的意图不起作用,我使用并创建了与示例中相同的类,类DisplayMessageActivity但问题仍然相同,这是我的一些代码。

public class Main extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

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


    }
    }
    catch(Exception e){
          System.out.println("fout zit hier");
    }

}

@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 */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
     }
} 

1 个答案:

答案 0 :(得分:2)

尝试替换Intent构造函数中的第一个参数。

Intent intent = new Intent(this, DisplayMessageActivity.class); // wrong
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class); // right

第一个参数是Context对象,并且记住片段不会从Context继承,因此解决方案是从父活动中获取一个。