向数据库添加值时出现android NullPointerException
我正在尝试为数据库添加一些值,如下所示:向数据库添加值时出现android NullPointerException,android,nullpointerexception,android-room,Android,Nullpointerexception,Android Room,我正在尝试为数据库添加一些值,如下所示: fab1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /*Intent i = new Intent(PlacesActivity.this, AddItemActivity.class); startActivity(i); finish();*/
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
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);
}
@Dao
公共接口数据库接口{
@查询(“从保存的位置选择*)
列出getAllItems();
@插入
void insertAll(PlaceSaved…placeSaveds);
@删除
作废删除(PlaceSaved…placeSaveds);
@更新
作废更新(PlaceSaved…placeSaveds);
}
我不确定这是否重要,我正在使用数据库的ROOM持久性库。您不能使用
AlertDialog.setView(R.layout.place\u add\u 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,因为现在您的EditTexts在Dialogbox中
这样试试看
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”方法的代码。该代码已更新。而且,正如我所说,如果我使用活动,数据库正在工作,可以在以下位置找到活动:,即AddItemActivity
您如何知道数据没有进入数据库?好的,您是否尝试通过重新加载应用程序来检查数据?您好,感谢您的回复,请查看Lucifer回答的评论。