java - 在适配器内打开一个对话框返回空值

标签 java android android-layout

我已经尝试让它工作大约 10 个小时但没有成功,我已经查看了堆栈溢出。我正在尝试显示一个简单的对话框并处理一些简单的输入。

它有一个TextView作为标题,EditText作为输入字段,以及两个按钮继续操作或取消。

出于某种原因,无论我做什么,我似乎都无法引用我的 shopping_dialogue xml 布局中的任何 View 。它给了我这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

我尝试向按钮添加一个 onClick 事件以查看是否有效,当我尝试在 onClick 方法中引用该按钮时,它给了我同样的错误。我正在使用一个 fragment 来执行此操作。

shopping_dialog.xml

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

<TextView
    android:id="@+id/shoppingDialogTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Edit Shopping List Name" />

<EditText
    android:id="@+id/shoppingDialogEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="30"
    android:inputType="textPersonName"
     />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/shoppingDialogOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/shoppingDialogCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="k"
        android:text="Button" />
</LinearLayout>

shopping_list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
card_view:cardCornerRadius="4dp"
android:id="@+id/shoppingList_card_view"
android:layout_margin="10dp"
android:layout_height="200dp">
<LinearLayout
    android:id="@+id/shoppingListParent"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center"

    android:layout_height="match_parent">
    <TextView
        android:id="@+id/shoppingName"
        android:layout_width="wrap_content"
        android:gravity="center"
        android:textSize="25sp"
        android:textColor="#FFF"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/shoppingDate"
        android:layout_width="wrap_content"
        android:gravity="center"
        android:textSize="25sp"
        android:textColor="#FFF"
        android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>

fragment_shopping_list.xml

  <?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>

ShopingAdapter.java

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);

    }


}

}

ShoppingFragment.java

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");
}



}

ShoppingData.java

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;
}

}

一切正常,直到我尝试引用 shopping_dialog.xml 中的任何内容。

如果您查看我的购物适配器类,我想要做的就是向 onBindViewHolder 方法内的“shoppigName” TextView 添加一个 onClick 监听器。这是有效的,但是当我尝试引用 shoppingDialogEditText 时,它会导致应用程序崩溃并给我该错误。

就像我提到的,我还尝试将点击事件方法 k 添加到 shopping_dialog.xml 内的按钮,并在我的 fragment 类中实现它,看看是否可行,但仍然会导致同样的错误。

最佳答案

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();


        }
    });

您用来查找edittext的 View 不是该对话框的 View ,因此它抛出空指针。

关于java - 在适配器内打开一个对话框返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58795966/

相关文章:

java - Android Retrofit 2 发布请求响应以及响应类名称

android - Seekbar 拇指图像被截断

Android TextView 右对齐

android - 将 ImageView 放在 Button android 上

java - intellij 中的 URL myweb_war_exploded 出现问题

java - 初始化静态最终变量

android - SDK 管理器中缺少 Google Play 服务

android - RelativeLayout 中的堆叠按钮

java - 适配器类 : nested in activity?

JAVA IF else 变量