Java 在适配器内打开返回空值的对话框
我已经试着让它工作了大约10个小时,但没有成功,我已经查看了所有的堆栈溢出。我正在尝试获得一个简单的对话框,以显示并处理一些简单的输入 它有一个文本视图作为标题,编辑文本作为输入字段,还有两个按钮继续操作或取消 出于某种原因,无论我做什么,我似乎都无法引用购物对话xml布局中的任何视图。它给了我这个错误: java.lang.NullPointerException:尝试调用虚拟方法 上的“void android.widget.TextView.setText(java.lang.CharSequence)” 空对象引用 我曾尝试向按钮添加一个onClick事件,看看它是否有效,当我尝试在onClick方法中引用按钮时,它会给我相同的错误。我用一个片段来做这个 购物对话.xml 一切正常,直到我尝试引用shopping\u dialog.xml中的任何内容 如果您查看我的购物适配器类,我试图做的是在onBindViewHolder方法内的“ShoppingName”文本视图中添加一个onClick侦听器。这是可行的,但当我尝试引用shoppingDialogEditText时,它会导致应用程序崩溃,并给我这个错误 正如我提到的,我还尝试向shopping_dialog.xml中的按钮添加一个click-event方法k,并在片段类中实现它,以查看该方法是否有效,但仍然会导致相同的错误Java 在适配器内打开返回空值的对话框,java,android,android-layout,Java,Android,Android Layout,我已经试着让它工作了大约10个小时,但没有成功,我已经查看了所有的堆栈溢出。我正在尝试获得一个简单的对话框,以显示并处理一些简单的输入 它有一个文本视图作为标题,编辑文本作为输入字段,还有两个按钮继续操作或取消 出于某种原因,无论我做什么,我似乎都无法引用购物对话xml布局中的任何视图。它给了我这个错误: java.lang.NullPointerException:尝试调用虚拟方法 上的“void android.widget.TextView.setText(java.lang.CharSe
viewHolder.shoppingName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View dailogView = LayoutInflater.from(mContext).inflate(R.layout.shopping_dialog, null);
AlertDialog.Builder builder1 = new AlertDialog.Builder(v.getContext());
builder1.setCancelable(true);
builder1.setView(dailogView);
AlertDialog alert = builder1.create();
EditText text = dailogView.findViewById(R.id.shoppingDialogEditText);
text.setText("Some Text");
alert.show();
}
});
用于查找编辑文本的视图不是该对话框的视图,因此它抛出了空指针。您使用了错误的视图来查找编辑文本。它不是对话框的视图。它是正在单击的项目的视图,而不是对话框。 用这个
viewHolder.shoppingName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View dailogView =
LayoutInflater.from(mContext).inflate(R.layout.shopping_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setCancelable(true);
builder.setView(dailogView);
AlertDialog alert = builder.create();
EditText text = dailogView.findViewById(R.id.shoppingDialogEditText);
text.setText("Shopping Dialog Text");
alert.show();
}
});
}嗨,v.findViewById就是你做错事的地方谢谢你,兄弟,我花了好几个小时想弄清楚这一点。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/shoppingList_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
public class ShoppingAdapter extends RecyclerView.Adapter<ShoppingAdapter.ShoppingViewHolder> {
private List<ShoppingData> shoppingList;
private Context context;
public ShoppingAdapter(List<ShoppingData> shoppingList, Context context){
this.shoppingList = shoppingList;
this.context = context;
}
@NonNull
@Override
public ShoppingViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i){
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.shopping_list_row, viewGroup, false);
return new ShoppingViewHolder(itemView);
}
@Override
public void onBindViewHolder(ShoppingViewHolder viewHolder, int i){
ShoppingData data = shoppingList.get(i);
Random k = new Random();
int color = Color.argb(255,k.nextInt(255),k.nextInt(255),k.nextInt(255));
viewHolder.parent.setBackgroundColor(color);
viewHolder.shoppingName.setText(data.getName());
viewHolder.shoppingDate.setText(data.getDate());
viewHolder.shoppingName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(v.getContext());
builder1.setCancelable(true);
builder1.setView(R.layout.shopping_dialog);
AlertDialog alert = builder1.create();
/*
EditText text = v.findViewById(R.id.shoppingDialogEditText);
text.setText("Some Text");
This causes a null object reference
*/
alert.show();
}
});
}
@Override
public int getItemCount(){return shoppingList.size();}
public class ShoppingViewHolder extends RecyclerView.ViewHolder{
private TextView shoppingName, shoppingDate;
private LinearLayout parent;
public ShoppingViewHolder(View itemView){
super(itemView);
parent = itemView.findViewById(R.id.shoppingListParent);
shoppingName = itemView.findViewById(R.id.shoppingName);
shoppingDate = itemView.findViewById(R.id.shoppingDate);
}
}
}
public class ShoppingFragment extends Fragment {
private RecyclerView recyclerView;
private ShoppingAdapter shoppingAdapter;
private List<ShoppingData> shoppingList = new ArrayList<>();
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState){
shoppingList.add(new ShoppingData("k","12/12/12"));
shoppingList.add(new ShoppingData("k","12/12/12"));
shoppingList.add(new ShoppingData("k","12/12/12"));
shoppingList.add(new ShoppingData("k","12/12/12"));
shoppingList.add(new ShoppingData("k","12/12/12"));
shoppingList.add(new ShoppingData("k","12/12/12"));
View rootView = inflater.inflate(R.layout.fragment_shopping_list, container, false);
recyclerView = rootView.findViewById(R.id.shoppingList_recycler_view);
shoppingAdapter = new ShoppingAdapter(shoppingList, getActivity());
RecyclerView.LayoutManager manager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(shoppingAdapter);
return rootView;
}
public void k(View view){ //the click listern that causes a null object reference when referencing the button
Button button = view.findViewById(R.id.shoppingDialogOk);
button.setText("k");
}
}
package com.uhcl.recipe5nd.helperClasses;
public class ShoppingData {
private String name;
private String date;
public ShoppingData(String name, String date){
this.name = name;
this.date = date;
}
public String getDate(){
return this.date;
}
public String getName(){
return this.name;
}
}
viewHolder.shoppingName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View dailogView = LayoutInflater.from(mContext).inflate(R.layout.shopping_dialog, null);
AlertDialog.Builder builder1 = new AlertDialog.Builder(v.getContext());
builder1.setCancelable(true);
builder1.setView(dailogView);
AlertDialog alert = builder1.create();
EditText text = dailogView.findViewById(R.id.shoppingDialogEditText);
text.setText("Some Text");
alert.show();
}
});
viewHolder.shoppingName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View dailogView =
LayoutInflater.from(mContext).inflate(R.layout.shopping_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setCancelable(true);
builder.setView(dailogView);
AlertDialog alert = builder.create();
EditText text = dailogView.findViewById(R.id.shoppingDialogEditText);
text.setText("Shopping Dialog Text");
alert.show();
}
});