ActivityNotFoundException?

2019-01-09 03:53发布

I am getting an ActivityNotFoundException in the following code:

Main.java

Intent intent = new Intent();
     intent.setAction("com.test.app.TEST");
     startActivity(intent); // ActivityNotFoundException

Manifest.xml

<activity android:name=".MainActivity" android:theme="@android:style/Theme.Dialog">
        <intent-filter>
            <action android:name="com.test.app.TEST" />
        </intent-filter>
</activity>

10条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-09 04:16

At the very top of your AndroidManifest.xml, you'll see the package attribute

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.example"

and then, in the activity tag, you'll see the name attribute:

<activity
            android:name=".Something"

Make sure that the package name and activity name, when joined together, make the full package specification of your Activity i.e.

com.android.example + .Something = com.android.example.Something

Otherwise, you'll get a ActivityNotFoundException.

查看更多
做自己的国王
3楼-- · 2019-01-09 04:20

I got this error after moving an activity class from one package to another. Clean build solved it (Project -> Clean).

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-09 04:25

To launch an activity by a string definition, use:

Intent intent = new Intent();
intent.setComponent(
        new ComponentName("com.app", "com.app.activity.TheActivity"));
startActivity(intent);
查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-09 04:27

Be sure to declare your activity in the manifest.xml within the aplication:

<application>
    <activity android:name=".YourNewActivity"/>
</application>

To start the new Activity:

Intent intent = new Intent(main.this, YourNewActivity.class);
startActivity(intent);

Where main stands for the current activity,

查看更多
登录 后发表回答