[英]intent is undefined tried a lot , nothing works
I get error with Intent. 我在Intent中遇到错误。 It says: intent is undefined
. 它说: intent is undefined
。 I've been searching a lot of other posts and it still doesn't work. 我一直在搜索很多其他帖子,但它仍然无效。 Any ideas? 有任何想法吗?
for your knowledge, i develop my own app with the basic of android developpes. 为了您的知识,我开发了自己的应用程序与android developpes的基础。 Because my own intent didn't work, i used and created the exact same class as in the example, class DisplayMessageActivity but the problem remains the same, Here some of my code. 因为我自己的意图不起作用,我使用并创建了与示例中相同的类,类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);
}
}
}
Try replacing the first parameter on the Intent constructor. 尝试替换Intent构造函数中的第一个参数。
Intent intent = new Intent(this, DisplayMessageActivity.class); // wrong
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class); // right
The first parameter is a Context
object, and remember that a Fragment does not inherit from Context
, so the solution is getting the one from the parent Activity. 第一个参数是Context
对象,并且记住Fragment不从Context
继承,因此解决方案是从父Activity获取一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.