有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 java。单击按钮时出现lang.NullPointerException

我不熟悉Java/for Android编程,我正在使用AlertDialog,我想存储两个字符串,增加一个计数器,然后将其存储在SharedReferences中

AlertDialog

    public boolean createEvent(MenuItem item){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.create_new, null))
            .setTitle("Add A Custom Event")
            // Add action buttons
            .setPositiveButton("Create", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    EditText dateBoxBox = (EditText)findViewById(R.id.dateBox); //error thrown here
                    EditText nameBoxBox = (EditText)findViewById(R.id.nameBox);
                    String nameString = nameBoxBox.getText().toString();
                    String dateString = dateBoxBox.getText().toString();
                    eventAdd(nameString, dateString);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //do nothing
                }
            })
            .show();
    return true;

}

创建新的。xml

<EditText
    安卓:id="@+id/nameBox"
    安卓:inputType="textPersonName"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="16dp"
    安卓:layout_marginLeft="4dp"
    安卓:layout_marginRight="4dp"
    安卓:layout_marginBottom="4dp"
    安卓:hint="Name" />
<EditText
    安卓:id="@+id/dateBox"
    安卓:inputType="date"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="4dp"
    安卓:layout_marginLeft="4dp"
    安卓:layout_marginRight="4dp"
    安卓:layout_marginBottom="16dp"
    安卓:hint="Date"/>

这是我在尝试将这些内容提交给SharedReference时调用的函数

eventAdd()

public void eventAdd(String sName, String sDate) {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    int iEventCounter = settings.getInt("eventCounter", 0);
    editor.putInt("eventCounter", iEventCounter+1);
    editor.putString("event1Name", sName);
    editor.putString("event1Date", sDate);

    // Commit the edits!
    editor.commit();
    TextView testTextView = (TextView)findViewById(R.id.section_label);
    testTextView.setText(sName);
}

我可以在logcat中看到错误发生的地方,但无法找出原因

LogCat

FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.cistoran.electriccountdown.MainActivity$2.onClick(MainActivity.java:124)
        at com.安卓.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
        at 安卓.os.Handler.dispatchMessage(Handler.java:99)
        at 安卓.os.Looper.loop(Looper.java:137)
        at 安卓.app.ActivityThread.main(ActivityThread.java:5270)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:974)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:790)
        at dalvik.system.NativeStart.main(Native Method)

共 (1) 个答案

  1. # 1 楼答案

    final View view = inflater.inflate(R.layout.create_new, null)
    builder.setView(view);
    //and add other settings you've done builder.set...
    

    改变

    EditText dateBoxBox = (EditText)findViewById(R.id.dateBox);
    EditText nameBoxBox = (EditText)findViewById(R.id.nameBox);
    

    EditText dateBoxBox = (EditText)view.findViewById(R.id.dateBox);
    EditText nameBoxBox = (EditText)view.findViewById(R.id.nameBox);