简体   繁体   中英

How to start an activity using a custom intent?

I have a very simple application (an example from a textbook) that consists of 2 activities: The first activity UsingIntentActivity has a button. When this button is clicked it must lead to the second activity called SecondActivity which will show a text on the screen.

I can achieve this using startActivity(new Intent(this, SecondActivity.class));

However in the textbook where I met this example another form of the same method is used:

startActivity(new Intent("net.dreamingpixel.SecondActivity"));

And in the Manifest File a matching custom intent is created (as I understood):

    <activity
        android:name=".UsingIntentActivity"
        android:label="@string/title_activity_using_intent" >
        <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="Second Activity" >
        <intent-filter>
            <action android:name="net.dreamingpixel.SecondActivity" />

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

So there is an intent-filter and its category is set to DEFAULT . But when I try to run the app like this and click the button of the UsingIntentActivity the app crashes. In the log cat I can see the following messages:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute the method of the activity

And then it points to the call of the startActivity method that I wrote at the beginning of this post.

Did anyone have a similar problem? Did I make a mistake somewhere?

UPDATE: Also I noticed that there is a warning in the Manifest file on the line where I open the second activity tag. It says: Exported activity does not require permission

Maybe this warning has to do something with my problem..

As you send that you have created second activity in manifest file as per

startActivity(new Intent("net.dreamingpixel.SecondActivity"));

Here net.dreamingpixel.SecondActivity means, here you need to provide the activity name with the package you created in your project...

In manifest at the top you will find package name. You need to use that package name with your activity...

Here as per above code..

    net.dreamingpixel ----- is a package
      SecondActivity  ----- is an Activity in that package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM