Java 意图是未定义的,尝试了很多次,没有任何效果
我故意犯错误。它说:Java 意图是未定义的,尝试了很多次,没有任何效果,java,android,eclipse,android-intent,undefined,Java,Android,Eclipse,Android Intent,Undefined,我故意犯错误。它说:意图未定义。我已经搜索了很多其他的帖子,但仍然不起作用。有什么想法吗 据你所知,我用android Developppes的基础开发了自己的应用程序。因为我自己的意图不起作用,所以我使用并创建了与示例中完全相同的类,class DisplayMessageActivity,但问题仍然是一样的,这里是我的一些代码 public class Main extends ActionBarActivity { public final static String EXTRA_MES
意图未定义
。我已经搜索了很多其他的帖子,但仍然不起作用。有什么想法吗
据你所知,我用android Developppes的基础开发了自己的应用程序。因为我自己的意图不起作用,所以我使用并创建了与示例中完全相同的类,class 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);
}
}
}
尝试替换意图构造函数上的第一个参数
Intent intent = new Intent(this, DisplayMessageActivity.class); // wrong
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class); // right
第一个参数是
上下文
对象,请记住片段不是从上下文
继承的,因此解决方案是从父活动获取片段。Zowat het enige dat ik niet geprobeerd had,de error为nu weg。Enorm hard bedankt voor het snelle antwoord ^翻译:“几乎是我唯一没有尝试过的事情,现在错误消失了。非常感谢您的快速回复:“请使用英语:)@user3450930 Graag gedaan!安特沃德机场a.u.b?。。。不客气。更好的是,不要从片段开始活动。片段
应该是自包含的和可重用的,因此不应该知道其他应用程序组件。片段
应该使用回调接口来告诉父级活动
启动另一个活动
。@Squonk我想在任何情况下都不会吗?例如,我有一个ListFragment
在其ListView
中显示文件列表,并在单击项目时启动广播Intent
。我使用getActivity()
作为上下文
,因为我知道这个片段不会被重用。。在这一点上我错了吗?在这里,传统是使用英语,并正确格式化源代码。