处理在 MainActivity 中创建意图时出现 NullPointerException

NullPointerException while dealing with creating an intent in MainActivity

几天来我一直在研究这个问题,但似乎找不到专门针对我的问题的线索。每次我尝试向我的捆绑包添加字符串时,我的应用程序都会崩溃。 我在 extras.putString(EXTRA_MESSAGE, message);

收到错误

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> priceGroupSpinnerItems;
    public static final String EXTRA_MESSAGE = "lss.mywebsiteoffline.MESSAGE";
    public String message = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spnPriceGroup = (Spinner) findViewById(R.id.price_group_spinner);

        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.list_price_group, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnPriceGroup.setAdapter(adapter);

        message = spnPriceGroup.getSelectedItem().toString();
    }

    /** Called when the user taps the Go button */
    public void sendMessage(View view) {

        Intent mIntent;

        mIntent = new Intent(this, nextActivity.class);
        Bundle extras = mIntent.getExtras();
        extras.putString(EXTRA_MESSAGE, message);
        mIntent.putExtras(extras);
        startActivity(mIntent);

    }
}

Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
Process: lss.mybbofflinever3, PID: 8181
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10896)
at android.view.View$PerformClick.run(View.java:22546)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5702) 
at android.widget.TextView.performClick(TextView.java:10896) 
at android.view.View$PerformClick.run(View.java:22546) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Bundle.putString(java.lang.String, java.lang.String)' on a null object reference
at lss.mywebsiteoffline.MainActivity.sendMessage(MainActivity.java:59)

我已经尝试过仅使用 intent 或 bundle 的示例,并将字符串直接添加到 putString。我可能会遗漏一些明显的东西。在将字符串直接添加到 putString 方法并得到相同的错误后,不确定我可能在寻找什么。

默认情况下,Intent 没有额外内容,getExtras() returns 为空。如果你想设置 extras,你需要自己创建 extras bundle。替换

Bundle extras = mIntent.getExtras();

Bundle extras = new Bundle();

或者只调用 Intent 上的 putExtra() 重载之一 - 它也会初始化 extras 包,以防它不存在。

试试这个:

Intent mIntent = new Intent(this, nextActivity.class);
mIntent.putExtra(EXTRA_MESSAGE, message);
startActivity(mIntent);