如何使用自定义意图启动活动? [英] How to start an activity using a custom intent?

查看:80
本文介绍了如何使用自定义意图启动活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序(一个教科书中的示例),它包含2个活动:第一个活动UsingIntentActivity有一个按钮.单击此按钮时,它必须导致名为SecondActivity的第二个活动,它将在屏幕上显示文本.

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.

我可以使用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"));

并且在Manifest File中创建了一个匹配的自定义意图(据我了解):

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" />

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

因此有一个意图过滤器,其类别设置为DEFAULT.但是,当我尝试像这样运行应用程序并单击UsingIntentActivity的按钮时,应用程序崩溃.在日志猫中,我可以看到以下消息:

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

然后它指向我在本文开头编写的startActivity方法的调用.

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?

更新:另外,我注意到在打开第二个活动标签的行的Manifest文件中有一个警告.它说:导出的活动不需要许可

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"));

这是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.

这篇关于如何使用自定义意图启动活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆