android.content.ActivityNotFound在android中调用新活动时出现异常



我得到异常

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);
        }
    });

也许我已经做了至少一千次了,但不知道为什么现在不能做。请帮帮我。

试试这个。。

您可以使用相同的代码。只需将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);
        }
    });

用这个来代替你的代码

Intent intent = new Intent(AliasTestActivity.this, SecondActivity.class);
startActivity(intent);

在清单中,像这样在第二个活动中声明

     <activity
        android:name=".SecondActivity"  >
     </activity>
<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>

然后再试一次。

如果您想从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);

使用此:

  Intent intent1 = new Intent(AliasTestActivity.this, SecondActivity.class);
  startActivity(intent1);

而不是设置组件。为什么要使用组件?

而不是这个:

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);
        }
    });

在100次尝试后,我在新的ComponentName()构造函数调用过程中修复了此ActivityNotFoundException,如下所示:

Intent intent = new Intent();
intent.setComponent(new ComponentName(context.getPackageName(), "YOUR_ACTIVITY_NAME_IN_MANIFEST"));
context.startActivity(intent);

相关内容

  • 没有找到相关文章

最新更新