Correctly addressing Intents to fix ActivityNotFoundException

173 views Asked by At

I get an ActivityNotFoundException when I use this code:

    public void addListenerOnButton3(){

    button3 = (Button) findViewById(R.id.btnSettings);
    button3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {    
    Intent intentSettings = new Intent("net.stuffilike.kanaflash.Settings");
    showToast("Settings clicked,");
    try{
    startActivity(intentSettings); 
    }
    catch(Exception e){
        showToastL("Exception" + e);
    }
    return;
}
});
}

Fair enough, except I can't tell how it wants me to tell it where the Activity is. Here is the relevant section of the Manifest:

   <activity
        android:name="net.stuffilike.kanaflash.Settings"
        android:label="@string/settings" >
        <intent-filter>
            <action android:name="android.intent.action.SETTINGS" />

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

How can I be sure the compiler finds my Settings.java file? Oh, my package is named

package net.stuffilike.kanaflash;
3

There are 3 answers

0
Ahmed Ekri On BEST ANSWER

try this

@Override
public void onClick(View arg0) {    
    Intent intentSettings = new Intent(X.this,Settings.class);
    showToast("Settings clicked,");
    try{
    startActivity(intentSettings); 
    }
    catch(Exception e){
        showToastL("Exception" + e);
    }
    return;
}
});

replace X with your current activity name ..

0
Steven Byle On

There's 2 ways you can do this.

  1. Use the action String to let the system see what Activitys can resolve the action on the Intent. If there is more than one Activity that can resolve the action, the user will get an option to pick which one they want.

    Intent intentSettings = new Intent("android.intent.action.SETTINGS");
    
  2. Open the Activity using its class directly (can only be done if both Activitys are in the same app).

    Intent intentSettings = new Intent(this, Settings.class);
    
0
Varun On

The constructor for new Intent(String action) takes action as paramter.

As per your manifest, the action you are using is android.intent.action.SETTINGS,

1.So your Intent should be as below

Intent intentSettings = new Intent("android.intent.action.SETTINGS");

or

2.You can directly invoke the activity by using the Activity name,

Intent intentSettings = new Intent(this, Settings.class);

or

3.You can also define a custom action like net.stuffilike.intent.action.SETTINGS and then use this to create your Intent like

Intent intentSettings = new Intent("net.stuffilike.intent.action.SETTINGS");