java 带有 Intent 的空对象引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34567175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Null Object Reference with Intent
提问by Malik
So thx for any awnsers given :)
the solution to my problem was, that my line
所以谢谢给定的任何 awnsers :)
我的问题的解决方案是,我的线路
Intent i = getIntent();
was not in my onCreate() function.
Thank you and have a nice day.
不在我的 onCreate() 函数中。
谢谢你,祝你有美好的一天。
我知道,这种问题在互联网上被问了很多,但我找不到任何对我有帮助的答案,所以我将把我的特例带到这里。
This is my function in my MainActivity, where i create the Intent and Start my second activity:
这是我在 MainActivity 中的函数,我在其中创建 Intent 并开始我的第二个活动:
private void showArticle(String entryUrl, String entryTitle) {
Intent intent = new Intent(this, ArticleActivity.class);
intent.putExtra(EXTRA_URL, entryUrl.toString());
intent.putExtra(EXTRA_TITLE, entryTitle.toString());
Log.d("DEBUG", intent.getStringExtra(EXTRA_URL));
Log.d("DEBUG", intent.getStringExtra(EXTRA_TITLE));
Log.d("DEBUG", "EXTRAS PUTTED");
startActivity(intent);
Log.d("DEBUG", "ACTIVITY STARTED");
}
This is the console output:
这是控制台输出:
01-02 15:29:21.808 7108-7108/com.example.myfirstapp D/DEBUG: http://example.com/correct-url
01-02 15:29:21.809 7108-7108/com.example.myfirstapp D/DEBUG: Yeah, we have the correct title hhere
01-02 15:29:21.809 7108-7108/com.example.myfirstapp D/DEBUG: EXTRAS PUTTED
01-02 15:29:21.809 7108-7108/com.example.myfirstapp I/Timeline: Timeline: Activity_launch_request id:com.example.myfirstapp time:1404863384
01-02 15:29:21.815 7108-7108/com.example.myfirstapp D/DEBUG: ACTIVITY STARTED
Sooo... 'til here everything works fine. Then, who guessed it, my second activity opens up:
Sooo...'直到这里一切正常。然后,谁猜到了,我的第二个活动打开了:
Intent i = getIntent();
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (i.getStringExtra(MainActivity.EXTRA_TITLE) == null) {
setTitle("MEEP");
} else {
setTitle(i.getStringExtra(MainActivity.EXTRA_TITLE));
}
...
But now my App crashes with this error message:
但是现在我的应用程序崩溃并显示此错误消息:
01-02 15:29:21.855 7108-7108/com.example.myfirstapp D/AndroidRuntime: Shutting down VM
01-02 15:29:21.870 7108-7108/com.example.myfirstapp E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 7108
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.article.ArticleActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access0(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.example.myfirstapp.article.ArticleActivity.onCreate(ArticleActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)?
at android.app.ActivityThread.access0(ActivityThread.java:156)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)?
at android.os.Handler.dispatchMessage(Handler.java:102)?
at android.os.Looper.loop(Looper.java:211)?
at android.app.ActivityThread.main(ActivityThread.java:5373)?
at java.lang.reflect.Method.invoke(Native Method)?
at java.lang.reflect.Method.invoke(Method.java:372)?
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Now my question is: why do i get a null pointer? i mean, the log clearly shows that the extra is put in the intent, why doesn't my second activity receive the key values? Can anyone help?
And thx for every tip.
for any more information, just write a comment :)
现在我的问题是:为什么我得到一个空指针?我的意思是,日志清楚地表明,额外的投入在意图中,为什么我的第二个活动没有收到关键值?任何人都可以帮忙吗?
并且感谢每个提示。
有关更多信息,只需写评论:)
采纳答案by Rusheel Jain
what is your "i" in second activity. Intent? If this intent is what you expect from main activity, then you should put this code in onNewIntent() if your activity is with SIngleTop flag, and this onNewIntent() should be overrriden.
你在第二个活动中的“i”是什么?意图?如果此意图是您对主要活动的期望,那么您应该将此代码放在 onNewIntent() 中(如果您的活动带有 SIngleTop 标志),并且此 onNewIntent() 应该被覆盖。
in onCreate(), to get this intent you should do something like this
在 onCreate() 中,要获得此意图,您应该执行以下操作
Intent i = getIntent()
回答by Yaw Asare
You are calling the method getStringExtra() on a null Object. To Access the Strings that you added to your intent use the getIntent() function.
您正在对空对象调用 getStringExtra() 方法。要访问您添加到意图中的字符串,请使用 getIntent() 函数。
Ex
前任
Bundle bundle = getIntent().getExtras();
if (bundle.getString(MainActivity.EXTRA_TITLE) == null) {
setTitle("MEEP");
} else {
setTitle(bundle.getString(MainActivity.EXTRA_TITLE));
}
回答by Pankaj
You are getting NullPointerException
saying java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
So, you have to check your parameter which you have passed via intent as below code:
你NullPointerException
说java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
所以,你必须检查你通过意图传递的参数,如下代码:
if (getIntent().getStringExtra(MainActivity.EXTRA_TITLE) == null) {
setTitle("MEEP");
} else {
setTitle(getIntent().getStringExtra(MainActivity.EXTRA_TITLE));
}