intent.getAction()将返回NULL [英] intent.getAction() is returning NULL

查看:1632
本文介绍了intent.getAction()将返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我添加了一个闪屏,我已经创造了飞溅的活动,并作出了manifest文件进行相应的更改。然而,当我现在运行我的应用程序,启动画面显示其分配的时间,然后返回一个NullPointerException异常。该问题是由引起

  intent.getAction()

在我的飞溅重定向到,intent.getAction类的241线()的返回null。这是我的理解是,动作从指定的活动清单文件抓起。这是不是它是否正确?如果是这样,有人可以看看这个,看看我有鸡奸的东西了?我认为没有错。

 <活动机器人:主NAME =
              机器人:标签=@字符串/ APP_NAME>    < /活性GT;   <活动机器人:SplashActivityNAME =机器人:主题=@安卓风格/ Theme.Black.NoTitleBar.Fullscreen>            &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>
            <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
            &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.GET_CONTENT/>            <数据机器人:mime类型=* / */>
            <类机器人:名字=android.intent.category.OPENABLE/>
            <类机器人:名字=android.intent.category.DEFAULT/>
        &所述; /意图滤光器>

的__ _ __ _ __ _ if语句被检索的动作 _ __ _ __ _ __ _ 的____

 意向意图= getIntent();    的System.out.println(意图动作是+ intent.getAction());
    如果(intent.getAction()。等于(Intent.ACTION_GET_CONTENT)){
        bimg [5] .setVisibility(View.GONE);
        mReturnIntent = TRUE;
 }否则如果(intent.getAction()。等于(ACTION_WIDGET)){
        Log.e(主,窗口小部件操作,串=+ intent.getExtras()的getString(文件夹));
        mHandler.updateDirectory(mFileMag.getNextDir(intent.getExtras()的getString(文件夹),真));    }
}


解决方案

的行动从启动该活动的意图抓住。清单意向过滤器定义什么样的意图将在除了那些直接指定活动为目标相匹配。

这是完全正常的行动为空,如果你有这样的事情开始了活动:

  startActivity(新意图(这一点,MyTargetActivity.class));

您没有指定意图的行动,所以没有之一。测试时所接收到的意图的行动,但是这是可以扭转这样的检查:

 如果(Intent.ACTION_GET_CONTENT.equals(intent.getAction())){
    // ...
}

,以避免需要一个明确的空检查,由于常量 Intent.ACTION_GET_CONTENT 不会为null。

(请注意,code仍然必须做一些合理的,即使你没有行动的检查在这样的情况匹配。)

In my app, I have added a splash screen and I have created the splash activity and made the appropriate changes to the manifest file. However, when I now run my app, the splash screen displays for its allotted time, and then returns a NullPointerException. The problem is being caused by

 intent.getAction()

On line 241 of the class my splash redirects to, intent.getAction() is returning null. It is my understanding that the action is grabbed from the manifest file for the specified activity. That is correct isnt it? If so, can someone look at this and see if I have buggered something up? I see nothing wrong.

 <activity android:name=".Main"
              android:label="@string/app_name">       

    </activity>

   <activity android:name=".SplashActivity" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

            <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />

            <data android:mimeType="*/*" />
            <category android:name="android.intent.category.OPENABLE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

_________If statement that is retrieving the action______________

 Intent intent = getIntent();

    System.out.println("Intent action is " + intent.getAction());
    if(intent.getAction().equals(Intent.ACTION_GET_CONTENT)) {
        bimg[5].setVisibility(View.GONE);
        mReturnIntent = true;
 } else if (intent.getAction().equals(ACTION_WIDGET)) {
        Log.e("MAIN", "Widget action, string = " + intent.getExtras().getString("folder"));
        mHandler.updateDirectory(mFileMag.getNextDir(intent.getExtras().getString("folder"), true));

    }
}

解决方案

The action is grabbed from the Intent that started the activity. The manifest intent-filters define what kinds of intents will be matched in addition to those that directly specify that activity as a target.

It's entirely normal for the action to be null if you started the activity with something like this:

startActivity(new Intent(this, MyTargetActivity.class));

You didn't specify an action for the Intent, so there isn't one. When testing the received intent's action, it's often useful to reverse the check like this:

if (Intent.ACTION_GET_CONTENT.equals(intent.getAction())) {
    // ...
}

to avoid the need for an explicit null check, since the constant Intent.ACTION_GET_CONTENT isn't going to be null.

(Note that your code will still have to do something reasonable even if none of your action checks match in a case like this.)

这篇关于intent.getAction()将返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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