web-dev-qa-db-fra.com

NullPointerException: tentative d'appel de la méthode virtuelle 'int Java.util.ArrayList.size ()' sur une référence d'objet null

Je suis novice en matière d'adaptateurs Fragments et ListView personnalisés. Quelqu'un peut-il me donner un coup de main s'il vous plaît?

J'ai mon Fragment où j'ai mon ListView

public class RecordingListFragment extends Fragment implements OnItemClickListener {

    ListView mListView;
    TextView emptyView;
    Button options, delete,edit;
    ArrayList<String> recordings = null;
    RecordingsListAdapter mAdapter;

    public RecordingListFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.recording_list_fragment, container,false);

        options = (Button) rootView.findViewById(R.id.options);
        edit = (Button) rootView.findViewById(R.id.edit);
        delete = (Button) rootView.findViewById(R.id.delete);
        mListView = (ListView) rootView.findViewById(R.id.list);
        emptyView = (TextView) rootView.findViewById(R.id.empty);


        mAdapter = new RecordingsListAdapter(this, recordings);
        mListView.setAdapter(mAdapter);
        mListView.setEmptyView(emptyView);
        mListView.setOnItemClickListener(this);

        return rootView;
    }
}

et mon xml pour ça

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    xmlns:Android1="http://schemas.Android.com/apk/res/Android"
    Android:id="@+id/recording_list_fragment"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:background="#ffffff"
    Android:orientation="vertical">

    <RelativeLayout
        Android:layout_height="50dp"
        Android:layout_width="match_parent"
        Android:orientation="horizontal"
        Android:background="@drawable/title" >

        <TextView
            Android1:id="@+id/title"
            Android1:layout_width="wrap_content"
            Android1:layout_height="50dp"
            Android1:layout_alignParentTop="true"
            Android1:layout_centerHorizontal="true"
            Android1:layout_marginStart="80dp"
            Android1:gravity="center"
            Android1:text="@string/app_name"
            Android1:textAlignment="center"
            Android1:textColor="#000000"
            Android1:textStyle="bold" />

    </RelativeLayout>

    <FrameLayout
        Android:layout_height="wrap_content"
        Android:layout_width="match_parent">

        <ListView
            Android:id="@+id/list"
            Android:animateLayoutChanges="true"
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"/>
        <!--  Android:divider="@null"--> 

        <TextView
            Android:id="@+id/empty"
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:gravity="center"
            Android:text="@string/empty_list_text"
            Android:paddingLeft="30dp"
            Android:paddingRight="30dp"
            Android:paddingTop="180dp" />

    </FrameLayout>
</LinearLayout>

et j'ai mon élément de liste personnalisé

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

    <CheckedTextView
        Android:id="@+id/textView"
        Android:layout_width="fill_parent"
        Android:layout_height="?android:attr/listPreferredItemHeight"
        Android:textAppearance="?android:attr/textAppearanceLarge"
        Android:gravity="center_vertical"
        Android:checkMark="?android:attr/listChoiceIndicatorSingle"
        Android:paddingLeft="6dip"
        Android:paddingRight="6dip"
        Android:textColor="#000000"
        /> 

</LinearLayout>

et enfin, mon Adapter

public class RecordingsListAdapter  extends BaseAdapter {
    Context ctx;
    LayoutInflater lInflater;
    ArrayList<String> recordings;

    RecordingsListAdapter(Context context, ArrayList<String> products) {
        ctx = context;
        recordings = products;
        lInflater = (LayoutInflater) ctx
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public RecordingsListAdapter(RecordingListFragment recordingListFragment,
    ArrayList<String> recordings) {
        // TODO Auto-generated constructor stub
    }


    @Override
    public int getCount() {
        return recordings.size();
    }


    @Override
    public Object getItem(int position) {
        return recordings.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.custom_list_item, parent, false);
        }

        return view;
    }
} 

et quand je lance ça, j'obtiens ce pointeur nul

11-26 11:00:45.352: E/AndroidRuntime(10191): Java.lang.RuntimeException: Unable to start activity ComponentInfo{d/recordings.RecordingsActivity}: Java.lang.NullPointerException: Attempt to invoke virtual method 'int Java.util.ArrayList.size()' on a null object reference
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2298)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.os.Handler.dispatchMessage(Handler.Java:102)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.os.Looper.loop(Looper.Java:135)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.ActivityThread.main(ActivityThread.Java:5221)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Java.lang.reflect.Method.invoke(Native Method)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Java.lang.reflect.Method.invoke(Method.Java:372)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:899)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
11-26 11:00:45.352: E/AndroidRuntime(10191): Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'int Java.util.ArrayList.size()' on a null object reference
11-26 11:00:45.352: E/AndroidRuntime(10191):    at recordings.RecordingsListAdapter.getCount(RecordingsListAdapter.Java:36)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.widget.ListView.setAdapter(ListView.Java:487)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at recordings.RecordingListFragment.onCreateView(RecordingListFragment.Java:47)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.Fragment.performCreateView(Fragment.Java:2053)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.FragmentManagerImpl.moveToState(FragmentManager.Java:894)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1067)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.BackStackRecord.run(BackStackRecord.Java:833)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.FragmentManagerImpl.execPendingActions(FragmentManager.Java:1452)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.Activity.performStart(Activity.Java:5948)
11-26 11:00:45.352: E/AndroidRuntime(10191):    at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2261)
11-26 11:00:45.352: E/AndroidRuntime(10191):    ... 10 more

Merci d'avance!!!

10
android enthusiast

Vous devez initialiser vos enregistrements. Vous passez à l'adaptateur null

ArrayList<String> recordings = null; //You are passing this null
17
Unii

Ce problème est dû au fait que la variable ArrayList n'est pas instanciée. Besoin de déclarer la variable "enregistrements" comme ci-dessous, cela devrait résoudre le problème;

ArrayList<String> recordings = new ArrayList<String>();

cela appelle le constructeur par défaut et affecte une chaîne vide à la variable d'enregistrements afin qu'elle ne soit plus nulle.

7
ameyx

Changement

 mAdapter = new RecordingsListAdapter(this, recordings);

à

 mAdapter = new RecordingsListAdapter(getActivity(), recordings);

et assurez-vous également que recordings!=null dans mAdapter = new RecordingsListAdapter(this, recordings);

6
M D