Android意向返回Nullpointerexception

| 我希望获得有关如何在传递意图时解决NullPointerException问题的指导。我想创建一个列表视图,可以在其中单击以显示一些相关信息。因此,基本上,主列表可以运行,但是当我单击该条目时,它就会崩溃。 我已经检查过LogCat,它说我有Nullpointerexception,我不熟悉如何检查/查找Null变量。我认为我的mRowId为null,但我不知道如何使其成为已定义的值。 有人可以帮我发现下面的意图问题吗? 传递意图的主要文件是这个。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Intent i = new Intent(this, drugdetail.class);  
    i.putExtra(DrugsDbAdapter.KEY_ROWID, id);

    startActivity(i);
}
读取意图的第二个文件是这个。
package com.paad.medboxsd;

public class drugdetail extends Activity{

private TextView mDrugText;
private TextView mContentText;

private Long mRowId;

    DrugsDbAdapter.DatabaseHelper mDbHelper = new DrugsDbAdapter.DatabaseHelper(this);

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

        mDrugText = (TextView) findViewById(R.id.drug);
        mContentText = (TextView) findViewById(R.id.content);

        //potentially where the problem on Nullpointers lie where LogCat    says null pointer exception from fetchDrugs(). Meaning likely that mRowId is Null.
        //mRowId = savedInstanceState != null ? savedInstanceState.getLong(DrugsDbAdapter.KEY_ROWID) : null;

        Bundle extras = getIntent().getExtras();

        mRowId = extras.getLong(DrugsDbAdapter.KEY_ROWID);

        Cursor drug = mDbHelper.fetchDrug(mRowId);

        //Managing Cursor to pluck values to display
        startManagingCursor(drug);

        mDrugText.setText(drug.getString(drug.getColumnIndexOrThrow(DrugsDbAdapter.KEY_DRUG)));

        mContentText.setText(drug.getString(drug.getColumnIndexOrThrow(DrugsDbAdapter.KEY_CONTENT)));

    }
LogCat输出为:
05-26 15:27:48.698: ERROR/AndroidRuntime(4829): FATAL EXCEPTION: main
05-26 15:27:48.698: ERROR/AndroidRuntime(4829): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.medboxsd/com.paad.medboxsd.drugdetail}: java.lang.NullPointerException
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.os.Looper.loop(Looper.java:123)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at java.lang.reflect.Method.invoke(Method.java:521)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at dalvik.system.NativeStart.main(Native Method)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829): Caused by: java.lang.NullPointerException
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at com.paad.medboxsd.DrugsDbAdapter$DatabaseHelper.fetchDrug(DrugsDbAdapter.java:195)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at com.paad.medboxsd.drugdetail.onCreate(drugdetail.java:35)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     ... 11 more
    
已邀请:
您在这里设置mRowId
mRowId = savedInstanceState != null ? savedInstanceState.getLong(DrugsDbAdapter.KEY_ROWID) : null;
但是您立即在这里再次设置
    Bundle extras = getIntent().getExtras();
    mRowId = extras.getLong(DrugsDbAdapter.KEY_ROWID);
并使用它而不检查它是否为null。 做这个:
mRowId = (savedInstanceState != null) ?
    savedInstanceState.getLong(DrugsDbAdapter.KEY_ROWID) : null;
if (mRowId == null)
{
  Bundle extras = getIntent().getExtras();
  mRowId = (extras != null) ? extras.getLong(DrugsDbAdapter.KEY_ROWID) : null;
}

if (mRowId != null)
{
   //database stuff here
}
else
{
   //error handling here
}
    

要回复问题请先登录注册