intent.getAction() is returning NULL
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.)
![Steve Weaver](https://i.stack.imgur.com/bWI1r.jpg?s=256&g=1)
Steve Weaver
Android Developer - Java Developer (Aspiring) - Currently learning Javascript and Angular
Updated on July 28, 2022Comments
-
Steve Weaver almost 2 years
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)); } }
-
Steve Weaver over 11 yearsBut shouldn't the action be grabbed from the AndroidManifest.xml when the activity is launched? I am launching the activity just as you noted.
-
adamp over 11 yearsNo, see the edit at the top of the answer. The intent-filters are just filters defining what kinds of more general intents can be matched. If you startActivity for an intent that directly targets that one activity by name, it doesn't have an action unless you specifically give it one. intent-filters are never used to "fill in" additional info in an intent, the intent will always contain only what the intent's creator added to it.
-
Steve Weaver over 11 yearsMan...I was way off then! Thanks for your help! By chance do you have an example handy of setting an intent action?
-
adamp over 11 years
new Intent(Intent.ACTION_GET_CONTENT)
,myIntent.setAction(Intent.ACTION_GET_CONTENT)
, etc. :) developer.android.com/reference/android/content/Intent.html -
adamp over 11 yearsYou're welcome! Now drop the splash screen, just launch into your app. It's cleaner. ;)
-
C Williams over 5 yearsThis make lots of sense, cause the string value can never be null