In the following SSCCE I am trying to invoke an explicit intent from preferences.xml, to open an Activity which is located in the one and only package in the app, in which all Activity's are located.

But I get the following exception:

android.content.ActivityNotFoundException: No Activity found to handle Intent {  }

I have seen this question but it is about starting an Activity in another package, and somebody said in that question that their app to open Activity in default package works fine.

Following are the relevant parts of code.

NOTE: Since SecondActivity is in the same package as the MainActivity, I initially tried to use only one android:targetClass attribute to the <intent> in the preferences.xml, but then after the exception I added the android:targetPackage too, but that did not solve the problem.

MainActivty.java:

package practice.preferences_practice;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class MainActivity extends PreferenceActivity {

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <Preference android:key="@+id/preferences_preference_preferenceContainingIntent"
        android:title="@string/preferences_preference_preferenceContainingIntent_title"
        android:summary="@string/preferences_preference_preferenceContainingIntent_summary" >


        <intent android:targetPackage="practice.preferences_practice"
            android:targetClass="practice.preferences_practice.SecondActivity" />


    </Preference>

</PreferenceScreen>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="practice.preferences_practice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="23" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
    </application>

</manifest>

NOTE: I have Not used an <intent-filter> in Manifest for the SecondActivity because it lies in the same default package as the MainActivity, which is practice.preferences_practice.

NOTE: If you think I should post all the other code files as well, please let me know.




EDIT:

res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Preferences Practice</string>
    <string name="hello_world">Hello world!</string>

    <string name="preferences_preference_preferenceContainingIntent_title">Preferece Title</string>
    <string name="preferences_preference_preferenceContainingIntent_summary">Opens another activity because this preference contains and invokes an Intent.</string>

    <string name="title_activity_second">SecondActivity</string>

</resources>

res/layout/activity_second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F4A460"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

SecondActivity.java:

package practice.preferences_practice;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}
2

There are 2 answers

1
turbandroid On BEST ANSWER

You code is as it should be. It's working pretty fine as I also tested it. Try to clean build the project and reinstall the apk.

1
Gaurav On

Use startActivity(new Intent(MainActivity.this, SecondActivity.class)); to start second activity.