2

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);
    }
}
Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183
  • You are targeting SecondActivity but you are adding addPreferencesFromResource(R.xml.preferences); in MainActivity. – turbandroid Mar 09 '16 at 05:21
  • @O'one "you are adding addPreferencesFromResource(R.xml.preferences); in MainActivity" - because I want to add the preferences to the MainActivity. This is an example app and my MainActivity is the PreferenceActivity - it displays the list of preferences. "You are targeting SecondActivity" - Yes. The `MainActivity` displays a preference (there is only one preference item in this app, otherwise a _list of_ preferences would be displayed), and when the user clicks on the Preference (the only one in this case), the `SecondActivity` should open. **Am I missing something?** – Solace Mar 09 '16 at 05:28
  • add this inside preference and then intent tag. android:action="android.intent.action.VIEW" – Developine Mar 09 '16 at 05:34
  • also check if you are using fully qualified name. – Developine Mar 09 '16 at 05:37
  • also use only sub packages in the target class section. – Developine Mar 09 '16 at 05:40
  • 1
    @Solace your code seems fine, Keeping in mind the same use case you trying to implement I tried the same code and it's working fine. You can post other files to find the root cause of this issue. – turbandroid Mar 09 '16 at 05:53
  • 1
    @HammadTariqSahi (1) But why would I need an `action` for the intent? It is an explicit intent; I want to start one particular Activity (and I am providing its name), not-any-activity-which-could-handle-VIEW-intent. (2) I AM using fully qualified names as you can see in the preferences.xml file I provided (3) There is no sub-packages, only one package which contains all Activity's, as mentioned in the question. – Solace Mar 09 '16 at 05:56
  • @O'one OK posting them in the question. – Solace Mar 09 '16 at 05:56
  • 1
    http://stackoverflow.com/questions/12198831/preference-items-and-explicit-intents – Developine Mar 09 '16 at 06:00
  • @O'one I posted it all in the question. I also updated code of `MainActivity.java` to display package name and import statements – Solace Mar 09 '16 at 06:08
  • @HammadTariqSahi thank you for the link. Could you see the answer by Dj S, because the accepted answer shows an IMPLICIT intent (somebody has pointed it out there in a comment under the QUESTION)? Do you see anything in my code which differs from what is described in the answer by Dj S? Sorry if I am missing smething. – Solace Mar 09 '16 at 06:12
  • @O'one If you could run my code now and see if it works, I will be thankful – Solace Mar 09 '16 at 06:13
  • 1
    You code is as it should be. It's working pretty fine. Try to clean build the project and reinstall the apk. or connect with me over skype so I can check it by screen sharing. – turbandroid Mar 09 '16 at 06:33
  • 1
    @Solace the only difference is your declaration in manifest is not fully qualified, but i don't think it matters, try using fully qualified in manifest also, and then let me know. – Developine Mar 09 '16 at 06:43
  • 1
    @O'one Oh goodness it's working now. I did a clean build and restarted Eclipse as well. Thank you so so very much, I really appreciate your help. =) – Solace Mar 09 '16 at 06:49
  • @HammadTariqSahi Which declaration? That of `SecondActivity`? The `.` before `SecondActivity` means the compiler will automatically add the default package's full name there. And hey it is working now after clean build. Thank you for your time and help. =) – Solace Mar 09 '16 at 06:51
  • @Solace I would like to suggest you to use android studio for further development. – turbandroid Mar 09 '16 at 06:52
  • enjoy now :) and yes start using android studio. – Developine Mar 09 '16 at 06:55
  • @HammadTariqSahi It shows down my already slow laptop =( – Solace Mar 09 '16 at 07:00
  • :( if you are android developer upgrade laptop (ram) :) and enjoy android studio :P – Developine Mar 09 '16 at 09:44
  • @HammadTariqSahi Yeah I have 3GB RAM right now, I am thinking of upgrading it to 8GB. But somebody told me that it has only two cores in its processor (which is core i3), so it won't be too fast. – Solace Mar 09 '16 at 09:47
  • i have i5, but when my system ram was 4gb it was too slow, and when upgraded to 8gb, now fine. – Developine Mar 09 '16 at 09:48
  • @HammadTariqSahi See, you have i5. I'll have to buy a new laptop. This one has a slow processor too – Solace Mar 09 '16 at 09:50
  • yes you are right :) btw I have bookmarked your profile page :) will ask you whenever i am stuck in code :) – Developine Mar 09 '16 at 09:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105784/discussion-between-solace-and-hammad-tariq-sahi). – Solace Mar 09 '16 at 09:53

2 Answers2

2

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.

turbandroid
  • 2,296
  • 22
  • 30
0

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

Gaurav
  • 1,965
  • 2
  • 16
  • 32
  • This DOES NOT answer the question. I know how to start an Activity from another Activity programmatically. The question is about _why doesn't an Intent specified in preferences successfully start an Activity? What am I missing? Where am I making a mistake?_ I suggest you remove this answer before it gets downvoted. – Solace Mar 09 '16 at 05:30