我创建了2个活动。当我从第一个通过intent调用第二个活动时,第二个活动的onCreate方法不会被调用。尽管第一个活动的onCreate方法正常调用它应该是。 以下是第一项活动的代码。
package com.webesperto.webespertofirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
int counter;
Button add, sub;
TextView tView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
tView = (TextView) findViewById(R.id.textView1);
// Intent in = new Intent(AC);
add.setOnClickListener(this);
sub.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bAdd:
tView.setText(++counter + "");
try {
Intent nextIntentView = new Intent(MainActivity.this, ShowValueActivity.class);
nextIntentView.putExtra("key", "counter");
startActivity(nextIntentView);
}
catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.bSub:
tView.setText(--counter + "");
break;
default:
break;
}
}
}
第二项活动的代码。
package com.webesperto.webespertofirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ShowValue extends Activity {
TextView tv_showValue1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_value);
tv_showValue1 = (TextView) findViewById(R.id.tv_showValue);
Intent gotIntent = (Intent) this.getIntent();
Bundle gotBundle = gotIntent.getExtras();
tv_showValue1.setText(gotBundle.getString("key"));
}
}
的的Manifest.xml 的
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.webesperto.webespertofirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.webesperto.webespertofirstapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ShowValueActivity"
android:label="@string/title_activity_show_value" >
</activity>
</application>
</manifest>
答案 0 :(得分:7)
你有
<activity
android:name=".ShowValueActivity"
android:label="@string/title_activity_show_value" >
</activity>
而不是
<activity
android:name=".ShowValue"
android:label="@string/title_activity_show_value" >
</activity>
因为您的代码是
public class ShowValue extends Activity {
但是,为什么你的应用程序崩溃说NoActivityFoundException
??
答案 1 :(得分:2)
请查看清单 变化
<activity
android:name=".ShowValueActivity"
android:label="@string/title_activity_show_value" >
</activity>
到
<activity
android:name=".ShowValue"
android:label="@string/title_activity_show_value" >
</activity>
你的清单应该是这样的
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.webesperto.webespertofirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.webesperto.webespertofirstapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ShowValue"
android:label="@string/title_activity_show_value" >
</activity>
</application>
</manifest>
答案 2 :(得分:1)
在类文件中更改您的第二个活动名称,如下所示:
package com.webesperto.webespertofirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ShowValueActivity extends Activity {
TextView tv_showValue1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_value);
tv_showValue1 = (TextView) findViewById(R.id.tv_showValue);
Intent gotIntent = (Intent) this.getIntent();
Bundle gotBundle = gotIntent.getExtras();
tv_showValue1.setText(gotBundle.getString("key"));
}
}
它会起作用:))