如何修复Android中"尝试调用'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'在空对象引用上"错误



我一直在尝试为 Android 中的布局元素设置一个新的自定义 ArrayAdapter(我已经做了几十次(,但似乎每当我启动活动时,我都会在我的代码中的某处收到空对象引用错误。

我已经检查了每一个细节,似乎没有什么不寻常的。

My CustomAdapter file (CustomAdapterRangliste(:

package ba.unsa.etf.rma.klase;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import ba.unsa.etf.rma.R;
public class CustomAdapterRangliste extends ArrayAdapter<RangLista> {
    private Context mContext;
    private int resources;
    private ArrayList<RangLista> rang;
    public CustomAdapterRangliste(Context context, int res, ArrayList<RangLista> rang) {
        super(context, res, rang);
        mContext = context;
        resources = res;
        this.rang = rang;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(R.layout.rangliste, parent, false);
         //View view = inflater.inflate(R.layout.activity_igraj_kviz_akt, null);
        TextView textView = (TextView) convertView.findViewById(R.id.etRangliste);
        final RangLista rangIgraca = rang.get(position);
        textView.setText(rangIgraca.toString());
        return convertView;
    }
}

布局元素(rangliste.xml(

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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">
    <TextView
        android:id="@+id/etRangliste"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="696dp"
        android:text="TextView"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

我用来初始化和绑定自定义适配器的代码(在实现片段的活动中(:

  final ListView rangLista = (ListView) findViewById(R.id.lvRanglista);
        final ArrayList<RangLista> ranking = new ArrayList<>();
        final CustomAdapterRangliste rangAdapter = new CustomAdapterRangliste(this, R.layout.rangliste, ranking);
        rangLista.setAdapter(rangAdapter);

具有列表视图元素 (fragment_ranglista.xml( 的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragmenti.RanglistaFrag">
    <!-- TODO: Update blank fragment layout -->
    <ListView
        android:id="@+id/lvRanglista"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#4FD80000"
        android:scrollbars="vertical" />
</FrameLayout>

片段文件:

package ba.unsa.etf.rma.fragmenti;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ba.unsa.etf.rma.R;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link RanglistaFrag.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link RanglistaFrag#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RanglistaFrag extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    public RanglistaFrag.OnFragmentInteractionListener2 mListener;
    public void postaviListener (RanglistaFrag.OnFragmentInteractionListener2 mListener)
    {
        this.mListener=mListener;
    }
    public RanglistaFrag() {
        // Required empty public constructor
    }
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ba.unsa.etf.rma.fragmenti.RanglistaFrag.
     */
    // TODO: Rename and change types and number of parameters
    public static RanglistaFrag newInstance(String param1, String param2) {
        RanglistaFrag fragment = new RanglistaFrag();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_ranglista, container, false);
    }
    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
    /*    if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }*/
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        /*if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }*/
    }
    @Override
    public void onDetach() {
        super.onDetach();
       // mListener = null;
    }
    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener2 {
        // TODO: Update argument type and name
        public void azuriranjeRangliste();
    }
}

无法弄清楚我做错了什么,类似的问题都没有帮助我,我检查了遗漏的行但没有成功

该错误意味着当您调用此rangLista.setAdapter(rangAdapter);变量rangLista为 null。这可能以多种方式发生,但我首先要检查的是您是否正在膨胀正确的布局,因为很可能findViewById(R.id.lvRanglista);行返回 null。

检查布局文件是否包含列表视图,其中包含您在 findViewById 中使用的 id。

确保 TextView 与 R.layout.rangliste 文件中的 ID 链接

相关内容

最新更新