[英]How to insert a text from an EditText into a database?
我正在尝试从editText中获取文本并将其输入到数据库中。 一旦尝试提交文本,我遇到的问题就是NullPointerException
。 任何帮助,将不胜感激。
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
这是从我的MainActivity :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_add_task:
final EditText text = (EditText)findViewById(R.id.editText);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add New Task")
.setMessage("what do you want to do next")
.setView(R.layout.custom_view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String task;
task = text.getText().toString();
dbHelper.insertNewTask(task);
if(getFragmentRefreshListener()!=null) {
getFragmentRefreshListener().onRefresh();
}
loadTaskList();
}
})
.setNegativeButton("Cancel",null)
.create();
dialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
我的custom_view.xml :
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a task"
android:id="@+id/editText" />
我的activity_main.xml:
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tablayout"
android:background="@color/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:backgroundTint="@android:color/darker_gray"
android:visibility="visible">
<ListView
android:id="@+id/lstTask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="@string/tasks"
android:textSize="30sp" />
</android.support.v4.view.ViewPager>
好吧,因为我看不到MainActivity
布局的xml,所以这很可能是导致NPE的原因。
final EditText text = (EditText)findViewById(R.id.editText);
您在活动视图层次结构中搜索您的编辑文本时(假设对话框创建中显示的内容),您应该在对话框视图层次结构中进行搜索。
.setView(R.layout.custom_view)
在那里,您将该视图设置为对话框的视图,可能就是引起异常的原因。 尝试像这样在onClick方法中完善对编辑文本的引用:
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add New Task")
.setMessage("what do you want to do next")
.setView(R.layout.custom_view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final EditText text = ((AlertDialog)dialog).findViewById(R.id.editText);
String task;
task = text.getText().toString();
dbHelper.insertNewTask(task);
if(getFragmentRefreshListener()!=null) {
getFragmentRefreshListener().onRefresh();
}
loadTaskList();
}
})
.setNegativeButton("Cancel",null)
.create();
dialog.show();
这是解决我的问题的方法:
我更改了这一行:
final EditText text = ((AlertDialog)dialog).findViewById(R.id.editText);
对此:
final EditText text = (EditText) ((AlertDialog)dialog).findViewById(R.id.editText);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.