it-swarm.com.de

NullPointerException: Versuch, die virtuelle Methode 'int Java.util.ArrayList.size ()' für eine Nullobjektreferenz aufzurufen

Ich bin ein Neuling bei Fragments und benutzerdefinierten ListView Adaptern. Kann mir bitte jemand helfen?

Ich habe mein Fragment wo ich mein ListView habe

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

und mein xml dafür

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

und ich habe mein benutzerdefiniertes Listenelement

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

und schließlich mein 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;
    }
} 

und wenn ich dies ausführe, erhalte ich diesen Nullzeiger

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

Danke im Voraus!!!

10

Sie sollten Ihre Aufnahmen initialisieren. Sie übergeben den Wert null an den Adapter

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

Dieses Problem ist darauf zurückzuführen, dass die ArrayList-Variable nicht instanziiert wird. Die Variable "recordings" muss wie folgt deklariert werden, um das Problem zu lösen.

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

dies ruft den Standardkonstruktor auf und weist der Aufzeichnungsvariablen eine leere Zeichenfolge zu, sodass sie nicht mehr null ist.

7
ameyx

Veränderung

 mAdapter = new RecordingsListAdapter(this, recordings);

zu

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

und stellen Sie auch sicher, dass recordings!=null bei mAdapter = new RecordingsListAdapter(this, recordings);

6
M D