我是android新手,我正在从developer.android做教程。我正在做“开始另一个活动”部分。但我的应用程序无法正常工作,我得到“遗憾的是应用已停止工作”。 这是代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Proveravamo da li je verzija Androida Honeycomb ili veca da bi koristili ActionBar APIje
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
//Prikazi UP dugme u ActionBar
getActionBar().setDisplayHomeAsUpEnabled(true);
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent(); //Uzimamo intent koji je poslala MainActivity
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); //Uzimamo EXTRA_MESSAGE iz intenta koji smo prihvatili
//dodajemo textView
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
答案 0 :(得分:0)
试试这个;
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
CurrentActivity.this.startActivity(myIntent);
通过以下方式检索额外内容:[代码编辑]
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String value = intent.getStringExtra("key"); //if it's a string you stored.
[已添加]不要忘记在AndroidManifest.xml
:
答案 1 :(得分:0)
我发现了什么问题。这是代码的这一部分:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
//Prikazi UP dugme u ActionBar
getActionBar().setDisplayHomeAsUpEnabled(true);
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
答案 2 :(得分:-1)
首先是秋天,我可以从你的代码中看到
you are trying to inflate TextView directly in setContentView() which is wrong ,
You have to inflate the complete layout file that represents your Activity layout like;
setContentView(R.layout.yourXML_filename);
其次,
You need to define setContentView() at the first in onCreate() not at the end,
what i can see in this code.
像。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.yourXML_filename);
.......Do your stuff what you are doing and wanna do
.......
}
请告诉我这是否可以解决您的问题......