空指针异常(错误),未从XML文件的findById中获取值。应用程式当机

时间:2019-12-17 21:29:46

标签: java android nullpointerexception

我遇到了activity_request_list.xml的问题 和包含的文件是为此 屏幕上的activity_donation_list.xml正常运行,但是一旦我单击activity_request_list.xml的屏幕,应用就会崩溃 以下是请求回收程​​序未获取值的类,该类保留null。

    public class RequestList extends AppCompatActivity {
        FirebaseFirestore firestore;
        RecyclerView requestrecycler;
        ListRecyclerAdaptor adaptor;
        ArrayList<ViewItem> viewItemArrayList;
        Intent callIntent = new Intent(Intent.ACTION_CALL);

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_donation_list);
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE} , 101);

            firestore = FirebaseFirestore.getInstance();
            requestrecycler = findViewById(R.id.requestrecycler);
            viewItemArrayList = new ArrayList<>();
            requestrecycler.setHasFixedSize(true);
            requestrecycler.setLayoutManager(new LinearLayoutManager(this));
            loadDataFromFirestore();

        }

        public void makeCall(View view){
            if (ActivityCompat.checkSelfPermission( this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
                return;
            }
            TextView tv = (TextView) view;
            //System.out.println(tv.getText());
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:"+tv.getText()));
            startActivity(callIntent);
        }

        public void loadDataFromFirestore() {

            if (viewItemArrayList.size() > 0)viewItemArrayList.clear();

            firestore.collection("requests").get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {

                            for (QueryDocumentSnapshot doc : task.getResult()) {

                                ViewItem viewItem = new ViewItem(
                                        doc.getId(),
                                        doc.getString("FoodName"),
                                        doc.getString("FoodType"),
                                        doc.getString("Quatity"),
                                        doc.getString("Unit"),
                                        doc.getString("Contact_no"),
                                        doc.getString("Location"));

                                viewItemArrayList.add(viewItem);
                            }

                            adaptor = new ListRecyclerAdaptor(null,RequestList.this, viewItemArrayList);
                            requestrecycler.setAdapter(adaptor);

                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(RequestList.this, "Error to display stores", Toast.LENGTH_LONG).show();
                        }
                    });

        }
    }

以下是错误 error of null value in requestList

以上的回收站类别如下

class ListRecyclerAdaptor extends RecyclerView.Adapter<ListRecyclerAdaptor.RecyclerHolder> {

        DonationList donationList;
        RequestList requestList;
        ArrayList<ViewItem> viewItemArrayList;

        public ListRecyclerAdaptor(DonationList donationList,RequestList requestList, ArrayList<ViewItem> viewItemArrayList) {
            this.donationList = donationList;
            this.requestList = requestList;
            this.viewItemArrayList = viewItemArrayList;
        }
        @NonNull
        @Override
        public RecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View view = layoutInflater.inflate(R.layout.single_row, parent, false);

            return new RecyclerHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ListRecyclerAdaptor.RecyclerHolder holder, final int position) {

            holder.mfoodName.setText(viewItemArrayList.get(position).getFoodName());
            holder.mfoodType.setText(viewItemArrayList.get(position).getFoodType());
            holder.mquantity.setText(viewItemArrayList.get(position).getQuantity());
            holder.munit.setText(viewItemArrayList.get(position).getUnit());
            holder.mcontact.setText(viewItemArrayList.get(position).getContact());
            holder.mlocation.setText(viewItemArrayList.get(position).getLocation());

            holder.mcallclick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callUser(position);

                }
            });
        }

        private void callUser(final int position) {
            donationList.firestore.collection("donations").document(viewItemArrayList.get(position).getUserID())
                    .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        if (document.exists()) {
    //                            String num = document.getString("Contact_no");
    //                            donationList.callIntent.setData(Uri.parse(num));
    //                        if (ActivityCompat.checkSelfPermission( donationList.getBaseContext(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED){
    //                            donationList.startActivity(donationList.callIntent);
    //                        }

                        } else {
                            Log.d("Msg", "No such document");
                        }
                    } else {
                        Toast.makeText(donationList.getBaseContext(),"Error", Toast.LENGTH_LONG).show();
                    }
                }
        });

requestList.firestore.collection("requests").document(viewItemArrayList.get(position).getUserID())
                    .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        if (document.exists()) {
    //                            String num = document.getString("Contact_no");
    //                            donationList.callIntent.setData(Uri.parse(num));
    //                        if (ActivityCompat.checkSelfPermission( donationList.getBaseContext(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED){
    //                            donationList.startActivity(donationList.callIntent);
    //                        }

                        } else {
                            Log.d("Msg", "No such document");
                        }
                    } else {
                        Toast.makeText(requestList.getBaseContext(),"Error", Toast.LENGTH_LONG).show();
                    }
                }
            });

    }

        @Override
        public int getItemCount() {
            return viewItemArrayList.size();
        }

        class RecyclerHolder extends RecyclerView.ViewHolder {

            public TextView mfoodName, mfoodType, mquantity, munit, mcontact, mlocation;
            LinearLayout mcallclick;

            public RecyclerHolder(@NonNull View itemView) {
                super(itemView);
                mfoodName = itemView.findViewById(R.id.foodNameView);
                mfoodType = itemView.findViewById(R.id.foodTypeView);
                mquantity = itemView.findViewById(R.id.quantityView);
                munit = itemView.findViewById(R.id.unitView);
                mcontact = itemView.findViewById(R.id.contactView);
                mlocation = itemView.findViewById(R.id.locationView);
                mcallclick = itemView.findViewById(R.id.callclick);
            }
        }
    }

请帮忙!!我了解的是requestList不是从finById中获取值并持有Null,我不确定如何解决该问题。

activity_request_list.xml的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".RequestList"
    android:layout_margin="10dp">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/requestrecycler"/>

    <!--ListView
        android:id="@+id/food_D_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" /-->
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

确保在您的布局文件:requestrecycler中存在ID为activity_donation_list.xml的RecyclerView