android 向数据库添加值时出现 NullPointerException

android NullPointerException when adding value to database

我正在尝试向数据库添加一些值:

 fab1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        /*Intent i = new Intent(PlacesActivity.this, AddItemActivity.class);
        startActivity(i);
        finish();*/
        //Dialog
        AlertDialog.Builder placeLLDialog= new AlertDialog.Builder(PlacesActivity.this);
        placeLLDialog.setView(R.layout.place_add_dialog);

        final EditText todo = findViewById(R.id.placeN);
        final EditText time = findViewById(R.id.placell);
        final EditText longi = findViewById(R.id.placell2);
        final PlaceDatabase db = Room.databaseBuilder(getApplicationContext(), PlaceDatabase.class,"production")
            .build();
        placeLLDialog.setTitle("Add Place with Latitude and Longitude")
          .setPositiveButton("Add", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
               if(!todo.getText().toString().equals("") &&  //line 97 of the error
                  !time.getText().toString().equals("") &&
                  !longi.getText().toString().equals("")) {

                final PlaceSaved placeSaved = new PlaceSaved(todo.getText().toString(),
                    time.getText().toString(), longi.getText().toString());
                AsyncTask.execute(new Runnable() {
                  @Override
                  public void run() {
                    db.databaseInterface().insertAll(placeSaved);
                  }
                });
              }
            }
          })
          .setNegativeButton("Cancel", null);

        placeLLDialog.show();

这是给我的错误:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
   at PlacesActivity.onClick(PlacesActivity.java:97)
   at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:164)
   at android.app.ActivityThread.main(ActivityThread.java:6494)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

代码的第 97 行,正如代码中注释的那样,是一个空值检查。不明白为什么它会导致问题,以及我如何实际将值插入数据库。

对应的布局为:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/placeN"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".7"
            android:hint="@string/place_name"
            android:imeOptions="actionDone"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/placell"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".7"
            android:hint="@string/latitude"
            android:imeOptions="actionDone"
            android:singleLine="true" />

        <EditText
            android:id="@+id/placell2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".7"
            android:hint="@string/Longitude"
            android:imeOptions="actionDone"
            android:singleLine="true" />
    </LinearLayout>

当我使用来自 fab1 clicklistner(评论部分)的 activity 时,我能够添加数据库,但不能使用对话框。

更新 这是 DAO:

@Dao
public interface DatabaseInterface {
  @Query("SELECT * FROM placesaved")
  List<PlaceSaved> getAllItems();

  @Insert
  void insertAll(PlaceSaved... placeSaveds);
  @Delete
  void delete(PlaceSaved... placeSaveds);
  @Update
  void update(PlaceSaved... placeSaveds);
}

而且我不确定这是否重要,我正在为数据库使用 ROOM 持久性库。

您不能使用AlertDialog.setView(R.layout.place_add_dialog);

您首先需要像下面那样膨胀布局 -

LayoutInflater lf = LayoutInflater.from(YourActivity.this);
View dialog = lf.inflate(R.layout.place_add_dialog, null);

然后像-

一样设置对话框的视图
myAlertDialog.setView(dialog);

同样, 像这样找到您的观点 -

EditText editText = (EditText) dialog.findViewById(R.id.editText);

错误是合法的,您需要修改您的 findViewByID,因为现在您的编辑文本位于对话框中。

试试这个方法,

AlertDialog.Builder placeLLDialog= new AlertDialog.Builder(PlacesActivity.this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.place_add_dialog, null);

并像这样获取您的编辑文本,

final EditText todo = view.findViewById(R.id.placeN);

所以,最后用下面的代码替换你的 fab1.click() 方法的代码。

    fab1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder placeLLDialog = new AlertDialog.Builder(PlacesActivity.this);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.place_add_dialog, null);

            placeLLDialog.setCustomTitle(view);

            final EditText todo = view.findViewById(R.id.placeN);
            final EditText time = view.findViewById(R.id.placell);
            final EditText longi = view.findViewById(R.id.placell2);
            placeLLDialog.setTitle("Add Place with Latitude and Longitude")
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if (!todo.getText().toString().equals("") &&
                                    !time.getText().toString().equals("") &&
                                    !longi.getText().toString().equals("")) {

                                final PlaceSaved placeSaved = new PlaceSaved(todo.getText().toString(),
                                        time.getText().toString(), longi.getText().toString());
                                AsyncTask.execute(new Runnable() {
                                    @Override
                                    public void run() {
                                        db.databaseInterface().insertAll(placeSaved);
                                    }
                                });
                            }
                        }
                    })
                    .setNegativeButton("Cancel", null);

            AlertDialog alertDialog = placeLLDialog.create();
            alertDialog.show();
            alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            //***********
        }
    });