Intent.getInt()不适用于ICS,但适用于JB

时间:2012-10-31 10:53:54

标签: java android android-intent nullpointerexception

我在开始新活动时使用此代码发送参数:

Intent inputForm = new Intent(getActivity(), InputForm.class);

Bundle b = new Bundle();
b.putInt("item", Integer.parseInt(mItem.id)); //Your id
inputForm.putExtras(b); //Put your id to your next Intent
startActivity(inputForm);

我使用此代码读取inputForm Activity中的参数:

    Bundle b = getIntent().getExtras();
    if (b != null) {
        int value = b.getInt("item");
        ID = value;
    }

Toast.makeText(getApplication(), "MIJN ID:" + Integer.toString(ID), Toast.LENGTH_LONG).show();

当我在我的三星Tab上运行此代码时,10.1 GT-P7510 ID(以及我的三星Tab 2 10.1 3G GT-P5100)也是0,当我用JB在我的Galaxy S3上运行相同的代码时,代码工作正常。当我使用此代码时,我收到此错误:

Intent inputForm = new Intent(getActivity(), InputForm.class);
inputForm.putExtra("item", mItem.id);
startActivityForResult(inputForm, 0);

//Read Settings in new Activity

String message;

message = getIntent().getExtras().getString("item"); //<---Nullpointerexception
Toast.makeText(getApplication(), message,Toast.LENGTH_LONG).show();
  

E / AndroidRuntime(2274):java.lang.RuntimeException:无法启动   活动ComponentInfo {com.obat.tabtasks / com.obat.tabtasks.InputForm}:   显示java.lang.NullPointerException

提前致谢,

ObAt

2 个答案:

答案 0 :(得分:0)

尝试:

Intent inputForm = new Intent(getActivity(), InputForm.class);
inputFrom.putExtra("item",Integer.parseInt(mItem.id));
startActivity(inputForm);

答案 1 :(得分:0)

你能否在

中分割一行
   Intent intent = getIntent();
   Bundle bundle = intent.getExtras();
   String message = bundle.getString("passed");

再次检查NPE在哪里?