gpt4 book ai didi

java - 我的警报对话框自定义布局出现问题

转载 作者:行者123 更新时间:2023-12-01 10:46:21 25 4
gpt4 key购买 nike

制定待办事项列表。有两个问题,不太重要的问题是,由于某种原因,我看不到我设置为“标签”的 TextView。更重要的是,当我单击“创建新任务”按钮时,会弹出警报,我可以将值放入 EditText 框中,但是当我点击“创建”时,它会崩溃,并且出现 NullPointer 异常,提示我正在尝试调用getText() 对空对象引用。我无法弄清楚我是否错误地膨胀,或者我是否没有正确地将 EditTexts 链接到警报。烦人的是,我的编辑文本警报框可以很好地编辑现有列表项(我硬编码用于测试)。这是我的布局和 Activity ,我评论了它中断的行。对所有 Log.d 感到抱歉,我真的很想想象这一切是如何工作的。

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<EditText
android:layout_width="55dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/txtCreatePriority"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="25dp"
android:hint="1"
android:textAlignment="center" />

<EditText
android:layout_width="235dp"
android:layout_height="wrap_content"
android:id="@+id/txtCreateItemContent"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/txtCreatePriority"
android:layout_toEndOf="@+id/txtCreatePriority"
android:layout_marginLeft="15dp"
android:hint="Do Laundry"
android:textAlignment="center" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Priority"
android:id="@+id/lblPriority"
android:layout_above="@+id/txtCreatePriority"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="17dp"
android:textStyle="bold"
android:textSize="23dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Task to-do"
android:id="@+id/lblItemContent"
android:layout_above="@+id/txtCreateItemContent"
android:layout_toRightOf="@+id/lblPriority"
android:layout_toEndOf="@+id/lblPriority"
android:layout_marginLeft="65dp"
android:textStyle="bold"
android:textSize="23dp" />
</RelativeLayout>

Activity

public class MainActivity extends AppCompatActivity {

private ListDataSource ds;
private ListView listViewToDo;
private Button btnAddNew;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context context = this;
Log.d("MainActivity","Attempting to create data source");
try {
ds = new ListDataSource();
}
catch(Exception e)
{
e.printStackTrace();
Log.d("MainActivity","Failed to create data source");
}
btnAddNew = (Button)findViewById(R.id.btnAddNew);
Log.d("Main Activity","Attempting to link empty list view to on screen view");
listViewToDo = (ListView)findViewById(R.id.listOfLists);
Log.d("Main Activity", "Views linked, Attempting to set adapter to listView");
listViewToDo.setAdapter(new ListDataSourceAdapter(this, ds));
Log.d("Main Activity", "Successfully set Adapter");
btnAddNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("addItem", "Entered onclick, Attempting to create AlertDialog");
AlertDialog.Builder addItem = new AlertDialog.Builder(context);
Log.d("addItem", "AlertDialog Built, attempting to create inflater");
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addItem.setView(inflater.inflate(R.layout.create_item_layout, null));
Log.d("addItem", "inflater built linking text boxes");
final TextView txtCreatePriority = (TextView)findViewById(R.id.txtCreatePriority);
final TextView txtCreateCellContent = (TextView)findViewById(R.id.txtCreateItemContent);
final TextView lblPriority = (TextView)findViewById(R.id.lblPriority);
final TextView lblItemContent = (TextView)findViewById(R.id.lblItemContent);
addItem.setTitle("Create new item");
addItem
.setCancelable(false)
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("editText onClick", "in onClick method, preparing to add entry");
// This is where the code breaks
ds.getList().add(Integer.valueOf(txtCreatePriority.getText().toString()), new CellContent(Integer.valueOf(txtCreatePriority.getText().toString()) + 1, txtCreateCellContent.getText().toString().trim()));
Log.d("editText onClick", "added new entry");
ListDataSourceAdapter adapter = new ListDataSourceAdapter(context, ds);
Log.d("editText onClick", "reestablished link to adapter");
listViewToDo.setAdapter(adapter);
Log.d("editText onClick", "adapter set");
adapter.notifyDataSetChanged();
Log.d("editText onClick", "DataSetChanged");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = addItem.create();
alertDialog.show();
}
});
// add button listener
listViewToDo.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
{
AlertDialog.Builder editItem = new AlertDialog.Builder(context);
final EditText edittext = new EditText(context);
editItem.setTitle("Change item");
editItem
.setMessage("Set new todo item")
.setView(edittext)
.setCancelable(false)
.setPositiveButton("Submit", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
Log.d("editText onClick","in onClick method, preparing to remove previous entry");
ds.getList().remove(position);
Log.d("editText onClick", "removed previous entry");
ds.getList().add(position, new CellContent(position + 1, edittext.getText().toString().trim()));
Log.d("editText onClick", "added new entry");
ListDataSourceAdapter adapter = new ListDataSourceAdapter(context,ds);
Log.d("editText onClick","reestablished link to adapter");
listViewToDo.setAdapter(adapter);
Log.d("editText onClick", "adapter set");
adapter.notifyDataSetChanged();
Log.d("editText onClick", "DataSetChanged");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = editItem.create();
alertDialog.show();
}
});
}
}

错误

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

最佳答案

您正在 View v 上调用“(EditText)v.findViewById”。 View v 是 onClickListener 中传回的 View ,即按钮本身。由于该按钮中不包含 EditText,因此这些 View 为空。当您尝试访问它们时会崩溃。

我有点不确定带有编辑文本的布局在这段代码中的位置。是和listview在同一个布局中,还是在create_item_layout中?

如果是 create_item_layout,则需要在获取 EditText 之前对其进行扩充。使用您膨胀的 View 来 findViewById。

关于java - 我的警报对话框自定义布局出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34144024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com