我得到了异常
12-12 17:31:18.401: E/AndroidRuntime(18177): FATAL EXCEPTION: main
12-12 17:31:18.401: E/AndroidRuntime(18177): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.testalias.sp/com.example.testalias.sp.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1635)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1434)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.app.Activity.startActivityForResult(Activity.java:3430)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.app.Activity.startActivityForResult(Activity.java:3391)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.app.Activity.startActivity(Activity.java:3626)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.app.Activity.startActivity(Activity.java:3594)
12-12 17:31:18.401: E/AndroidRuntime(18177): at com.example.testalias.AliasTestActivity$2.onClick(AliasTestActivity.java:40)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.view.View.performClick(View.java:4354)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.view.View$PerformClick.run(View.java:17961)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.os.Handler.handleCallback(Handler.java:725)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.os.Looper.loop(Looper.java:137)
12-12 17:31:18.401: E/AndroidRuntime(18177): at android.app.ActivityThread.main(ActivityThread.java:5328)
12-12 17:31:18.401: E/AndroidRuntime(18177): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 17:31:18.401: E/AndroidRuntime(18177): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 17:31:18.401: E/AndroidRuntime(18177): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-12 17:31:18.401: E/AndroidRuntime(18177): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-12 17:31:18.401: E/AndroidRuntime(18177): at dalvik.system.NativeStart.main(Native Method)
我的清单代码
<?xml version="1.0" encoding="utf-8"?>
<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.example.testalias.AliasTestActivity"
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=".sp.SecondActivity"
android:label="@string/app_name" >
</activity>
</application>
我的代码
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.setComponent(new ComponentName("com.example.testalias.sp", "com.example.testalias.sp.SecondActivity"));
startActivity(intent1);
}
});
可能我已经做过至少一千次,但不知道为什么现在不能做。 请帮帮我。
答案 0 :(得分:1)
尝试使用此代替您的代码
Intent intent = new Intent(AliasTestActivity.this, SecondActivity.class);
startActivity(intent);
在第二个活动中的清单声明中
<activity
android:name=".SecondActivity" >
</activity>
答案 1 :(得分:1)
试试这个..
您可以使用与您相同的代码。只需将ComponentName
com.example.testalias.sp
内的包名称更改为com.example.testalias
,然后再试一次
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.setComponent(new ComponentName("com.example.testalias", "com.example.testalias.sp.SecondActivity"));
startActivity(intent1);
}
});
答案 2 :(得分:0)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testalias">
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".AliasTestActivity"
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=".sp.SecondActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
然后再试一次。
答案 3 :(得分:0)
如果要从AliasTestActivity调用SecondActivity,请将以下代码添加到代码中以调用新的Activity。
Intent intent1 = new Intent(AliasTestActivity.this, SecondActivity.class);
startActivity(intent1);
而不是
Intent intent1 = new Intent();
intent1.setComponent(new ComponentName("com.example.testalias.sp", "com.example.testalias.sp.SecondActivity"));
startActivity(intent1);
答案 4 :(得分:0)
使用此:
Intent intent1 = new Intent(AliasTestActivity.this, SecondActivity.class);
startActivity(intent1);
而不是设置组件。你为什么要使用组件?
答案 5 :(得分:0)
而不是:
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.setComponent(new ComponentName("com.example.testalias.sp", "com.example.testalias.sp.SecondActivity"));
startActivity(intent1);
}
});
替换为:
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
Intent intent1 = new Intent(AliasTestActivity.this, SecondActivity.class);
startActivity(intent1);
}
});
答案 6 :(得分:0)
经过100次尝试后,在新的ComponentName()构造函数调用期间,我修复了此ActivityNotFoundException:
Intent intent = new Intent();
intent.setComponent(new ComponentName(context.getPackageName(), "YOUR_ACTIVITY_NAME_IN_MANIFEST"));
context.startActivity(intent);