【问题标题】:android NullPointerException when adding value to database向数据库添加值时出现android NullPointerException
【发布时间】:2018-03-10 05:39:20
【问题描述】:

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

 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$3$1.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(注释部分)的活动时,我能够添加数据库,但不能使用对话框。

更新 这是 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 持久性库。

【问题讨论】:

    标签: android nullpointerexception android-room


    【解决方案1】:

    错误是合法的,您需要修改您的 findViewByID,因为现在您的 edittextes 在对话框中。

    试试这个方法,

    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);
                //***********
            }
        });
    

    【讨论】:

    • 您好,感谢您的回复。错误消失了,但它没有添加到数据库中。有什么线索吗?
    • @BaRud,请从您的数据库文件中发布“insertAll”方法的代码。
    • 更新了。而且,正如我所说,如果我使用活动,数据库正在工作,活动可以在:stackoverflow.com/questions/49200154/… 找到,即AddItemActivity
    • 你怎么知道数据没有进入数据库?
    • 好的,您是否尝试通过重新加载应用程序来检查数据?
    【解决方案2】:

    你不能使用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);
    

    【讨论】:

    • 您好,感谢您的回复,请查看路西法回答的评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多