在android开发中意图传递额外内容时的错误

时间:2016-03-11 21:27:59

标签: android android-intent fatal-error

我正在尝试将用户名从一个活动发送到另一个活动。我在第一个活动中有以下代码:

    etUsername = (EditText) findViewById(R.id.username);
    Intent parent = new Intent(Login.this, Parent_Home.class);
    parent.putExtra("username", etUsername.getText().toString());
    startActivity(parent);

然后尝试在第二个活动中检索它,如下所示:

   Button bChild, bChore, bRewards, bStats, bSettings;
   Bundle extras = getIntent().getExtras();
   String user = extras.getString("username");

然而,当我运行此应用程序崩溃时,我收到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.jack.pointcollector, PID: 13980
                                               java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.jack.pointcollector/com.jack.pointcollector.Parent_Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:148)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                   at com.jack.pointcollector.Parent_Home.<init>(Parent_Home.java:11)
                                                   at java.lang.Class.newInstance(Native Method)
                                                   at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:148) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

任何想法导致了什么?

1 个答案:

答案 0 :(得分:1)

您在字段初始值设定项上调用getIntent()。不要这样做。在您的getIntent()活动方法中调用super.onCreate()之后,请不要致电onCreate() - 或几乎任何活动方法。

this sample app中,我的启动器活动在其用于启动其他活动的Intent中打包了一个额外内容:

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
*/

package com.commonsware.android.extra;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class ExtrasDemoActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  public void showOther(View v) {
    Intent other=new Intent(this, OtherActivity.class);

    other.putExtra(OtherActivity.EXTRA_MESSAGE, getString(R.string.other));
    startActivity(other);
  }
}

然后我在OtherActivity中检索并使用该值...但直到onCreate()内:

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
*/

package com.commonsware.android.extra;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class OtherActivity extends Activity {
  public static final String EXTRA_MESSAGE="msg";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.other);

    TextView tv=(TextView)findViewById(R.id.msg);

    tv.setText(getIntent().getStringExtra(EXTRA_MESSAGE));
  }
}