J'espère obtenir des instructions sur la façon de résoudre les problèmes sur NullPointerException lors du passage des intentions. Je voulais créer une vue de liste sur laquelle je peux cliquer plus loin pour afficher des informations associées. Donc, fondamentalement, la liste principale est capable de fonctionner, mais quand je clique sur l'entrée, elle se bloque.Intents Android renvoyant Nullpointerexception
J'ai vérifié LogCat et il dit que j'ai une exception Nullpointer, je ne suis pas familier avec la façon de vérifier/rechercher des variables Null. Je pense que mon mRowId est nul mais je ne sais pas comment en faire une valeur définie.
Quelqu'un peut-il m'aider à déceler un problème dans l'intention de passer ci-dessous?
Le fichier principal qui transmet l'intention est le suivant.
@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);
}
Le deuxième fichier lisant l'intention est la suivante.
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)));
}
La sortie LogCat est:
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
Chaque fois qu'il ya une exception, il indique toujours le numéro de la ligne sur laquelle il se produit. – Reno
Fournir la sortie de logcat –
La sortie de logcat indique que l'exception Nullpointer est créée au fetchDrug(), je pense que c'est le mRowId que je passe est probablement une valeur nulle pour commencer ... – jamen