我正在尝试使用Spinner和两个EditText在Android中进行自定义DialogFragment,但是当我打开Dialog时出现错误。 这是我的DialogFragment类嵌入到活动中:
public static class DialogListaPeces extends DialogFragment {
private NuevaCaptura actividad;
private Spinner spinner;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
actividad = (NuevaCaptura) getActivity();
LayoutInflater inflater = actividad.getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(actividad);
builder.setView(inflater.inflate(R.layout.dialog_new_specimen, null));
SQLiteDatabase database = actividad.db.getReadableDatabase();
Cursor c = database.rawQuery("SELECT _id, nombre FROM Peces", null);
spinner = (Spinner) actividad.findViewById(R.id.spPeces_new_specimen);
spinner.setAdapter(new SimpleCursorAdapter(actividad, android.R.layout.simple_spinner_dropdown_item, c, new String[] { "nombre" }, new int[] { android.R.id.text1 }, SimpleCursorAdapter.NO_SELECTION));
c.close();
database.close();
builder.setPositiveButton(R.string.aceptar, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int pez = (int) spinner.getSelectedItemId();
float longitud = Float.parseFloat(((EditText) actividad.findViewById(R.id.etLongitud_new_specimen)).getText().toString());
float peso = Float.parseFloat(((EditText) actividad.findViewById(R.id.etPeso_new_specimen)).getText().toString());
actividad.ejemplares.add(new Ejemplar(new Pez(pez), longitud, peso));
actividad.llenarListaEjemplares();
}
});
builder.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DialogListaPeces.this.getDialog().cancel();
}
});
return builder.create();
}
}
和我得到的错误:
06-23 13:30:52.659: E/AndroidRuntime(26186): FATAL EXCEPTION: main
06-23 13:30:52.659: E/AndroidRuntime(26186): java.lang.NullPointerException
06-23 13:30:52.659: E/AndroidRuntime(26186): at com.ssoomm.pesca.NuevaCaptura$DialogListaPeces.onCreateDialog(NuevaCaptura.java:596)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:253)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.os.Handler.handleCallback(Handler.java:605)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.os.Handler.dispatchMessage(Handler.java:92)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.os.Looper.loop(Looper.java:137)
06-23 13:30:52.659: E/AndroidRuntime(26186): at android.app.ActivityThread.main(ActivityThread.java:4476)
06-23 13:30:52.659: E/AndroidRuntime(26186): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 13:30:52.659: E/AndroidRuntime(26186): at java.lang.reflect.Method.invoke(Method.java:511)
06-23 13:30:52.659: E/AndroidRuntime(26186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
06-23 13:30:52.659: E/AndroidRuntime(26186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
06-23 13:30:52.659: E/AndroidRuntime(26186): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在第596行检查NuevaCaptura.java
。这是NullPointerException
发生的地方。检查那里的核数并确保它们不是null
。
如果您需要帮助,您应该告诉粘贴代码中的那一行。