ActivityNotFoundException?

后端 未结 10 732
滥情空心
滥情空心 2020-12-01 14:25

I am getting an ActivityNotFoundException in the following code:

Main.java

Intent intent = new Intent();
     intent.setAction(\"com.tes         


        
相关标签:
10条回答
  • 2020-12-01 15:04

    There two types of intents in android framework, 1-Implicit intents that you are using,

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

    just add one line in intent filter

    <intent-filter>
            <action android:name="com.test.app.TEST" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    

    2- Explicit Intents

    Intent i=new Intent(CurrentActivity.this,WhereWeWantToGoActivity.class);
    

    startActivity(i);

    0 讨论(0)
  • 2020-12-01 15:05

    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);
    
    0 讨论(0)
  • 2020-12-01 15:09

    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,

    0 讨论(0)
  • 2020-12-01 15:13

    To be safe you can also call your new activity like this:

    Intent intent = new Intent();
    intent.setClass(this, THECLASSNAME);
    startActivity(intent); // 
    

    However, you must add the activity to the androidmanifest - and write a . in front of it, e.g.

    <activity android:name=".YOURACTIVITYNAME"></activity>
    
    0 讨论(0)
  • 2020-12-01 15:13

    I have some addition to the @Tom Pace answer. The answer is completely right, but to make it more clear:

    ActivityNotFoundException occurs because of absence of

    <category android:name="android.intent.category.DEFAULT" />
    

    Because when Android OS see this in the manifest file, understands that this activity can receive intent.

    The point ActivityNotFoundException thrown is that, when activity(intent-creator-activity) tries to create intent for other activity(intent-receiver-activity), Android OS sees there is intent for receiver activity but receiver activity does not receive anyone. Then Android OS returns null or empty intent to intent-creator-activity. And startActivity throws that exception.

    I have found a code from android developers to avoid this exception:

    // Verify the original intent will resolve to at least one activity
    if (sendIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(chooser);
    }
    

    Android Developers: Intent Filters

    0 讨论(0)
  • 2020-12-01 15:15

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

    0 讨论(0)
提交回复
热议问题