2017-02-25 64 views
0

我得到了以下錯誤:Android:初學者適配器故障。如何解決setAdapter導致NullPointerException異常

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.ListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 

我堅持,希望你能有所幫助。

ThingsDBThing(String what, String where)

ListFragment列表:

import ... 

public class ListFragment extends Fragment{ 
    private static ThingsDB thingsDB; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     thingsDB = thingsDB.get(this.getActivity()); 
     ListView listView= (ListView) getActivity().findViewById(R.id.thing_list_view); 
     ArrayAdapter<Thing> adapter = new ArrayAdapter<Thing>(this.getActivity(), R.layout.list_item, R.id.text1, thingsDB.getThingsDB()); 
     listView.setAdapter(adapter); 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) { 
     View v = inflater.inflate(R.layout.fragment_list, container, false); 
     return v; 
    } 
} 

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/fragment_list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/darkGrey" 
       android:orientation="vertical"> 
    <ListView 
     android:id="@+id/thing_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="12dp"> 
    </ListView> 
</LinearLayout> 

回答

0

要調用getview時的觀點仍然沒有充氣,檢查片段生命週期,onCreate()是之前叫做onCreateView() https://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)

onCreate: Added in API level 11 void onCreate (Bundle savedInstanceState) Called to do initial creation of a fragment. This is called after onAttach(Activity) and before onCreateView(LayoutInflater, ViewGroup, Bundle), but is not called if the fragment instance is retained across Activity re-creation (see setRetainInstance(boolean)).

onCreateView Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null (which is the default implementation). This will be called between onCreate(Bundle) and onActivityCreated(Bundle).

在這種情況下,你將有

import ... 

public class ListFragment extends Fragment{ 
    private static ThingsDB thingsDB; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     thingsDB = thingsDB.get(this.getActivity()); 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) { 
     View v = inflater.inflate(R.layout.fragment_list, container, false); 
     ListView listView= (ListView) v.findViewById(R.id.thing_list_view); 
     ArrayAdapter<Thing> adapter = new ArrayAdapter<Thing>(this.getActivity(), R.layout.list_item, R.id.text1, thingsDB.getThingsDB()); 
     listView.setAdapter(adapter); 
     return v; 
    } 
} 
+0

謝謝!也欣賞鏈接!美好的一天:) – pkv

+0

我很高興它的幫助:) – ayinloya