Home » Android » android – intent.getAction() is returning NULL

android – intent.getAction() is returning NULL

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

    }
}
How to&Answers:

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.)