我有一个AlertDialog。在onCreate中我试图膨胀xml布局,但是当我尝试打开Dialog时,活动崩溃了。我花了很多时间试图找出原因,但老实说,我不知道。
这是logcat:
05-26 11:53:32.927: E/AndroidRuntime(2430): FATAL EXCEPTION: main
05-26 11:53:32.927: E/AndroidRuntime(2430): Process: it.sii.mywaiter, PID: 2430
05-26 11:53:32.927: E/AndroidRuntime(2430): java.lang.NullPointerException
05-26 11:53:32.927: E/AndroidRuntime(2430): at it.sii.mywaiter.PiattoActivity$MenuQuantityDialogFragment.onCreateDialog(PiattoActivity.java:77)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.os.Handler.handleCallback(Handler.java:733)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.os.Handler.dispatchMessage(Handler.java:95)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.os.Looper.loop(Looper.java:136)
05-26 11:53:32.927: E/AndroidRuntime(2430): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 11:53:32.927: E/AndroidRuntime(2430): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 11:53:32.927: E/AndroidRuntime(2430): at java.lang.reflect.Method.invoke(Method.java:515)
05-26 11:53:32.927: E/AndroidRuntime(2430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 11:53:32.927: E/AndroidRuntime(2430): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 11:53:32.927: E/AndroidRuntime(2430): at dalvik.system.NativeStart.main(Native Method)
这是DialogFragment:
public class MenuQuantityDialogFragment extends DialogFragment {
//crea il dialogfragment
private NumberPicker np = null;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setRetainInstance(true);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.number_picker, null));
np = (NumberPicker) findViewById(R.id.numberPicker1);
np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
np.setMinValue(1);
np.setMaxValue(10);
np.setWrapSelectorWheel(false);
np.setValue(2);
builder.setMessage(R.string.piatto_dialog_message)
.setTitle(R.string.piatto_dialog_title)
.setPositiveButton(R.string.piatto_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
porzioni = np.getValue();
b_quantity.setText("Porzioni: "+ porzioni);
dismiss();
}
})
.setNegativeButton(R.string.piatto_dialog_annulla, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
return builder.create();
}
}
这是xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:3)
使用inflater返回的对象来查找NumberPicker
View view = inflater.inflate(R.layout.number_picker, null);
builder.setView(view);
np = (NumberPicker) view.findViewById(R.id.numberPicker1);
它不属于Activity
的视图层次结构(您正在寻找它)
答案 1 :(得分:2)
您需要引用特定NumberPicker
的{{1}}。
inflated view