所以给予任何芒果的thx :) 我的问题的解决方案是,我的行
Intent i = getIntent();
不在我的onCreate()函数中 谢谢你,祝你有个愉快的一天。
这是我在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");
}
这是控制台输出:
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 ......直到这里一切正常。然后,谁猜对了,我的第二个活动开启了:
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));
}
...
但现在我的应用程序崩溃了这个错误消息:
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.access$800(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.access$800(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)
现在我的问题是:为什么我得到一个空指针?我的意思是,日志清楚地显示了额外的意图,为什么我的第二个活动没有收到关键值?有人可以帮忙吗?
每个提示都是thx。
如需了解更多信息,请发表评论:)
答案 0 :(得分:3)
你的是什么"我"在第二次活动中。意图? 如果这个意图是您对主要活动的期望,那么您应该将此代码放在onNewIntent()中,如果您的活动是使用SIngleTop标志,并且应该覆盖此onNewIntent()。
在onCreate()中,要获得此意图,您应该执行类似这样的操作
Intent i = getIntent()
答案 1 :(得分:2)
您正在null对象上调用方法getStringExtra()。要访问您添加到意图中的字符串,请使用getIntent()函数。
前
Bundle bundle = getIntent().getExtras();
if (bundle.getString(MainActivity.EXTRA_TITLE) == null) {
setTitle("MEEP");
} else {
setTitle(bundle.getString(MainActivity.EXTRA_TITLE));
}
答案 2 :(得分:0)
您正在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));
}