Android:初学者适配器问题。如何解决 setAdapter 导致 NullPointerException
Android: Beginner Adapter Trouble. How to solve setAdapter causing 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
我被困住了,希望你能帮忙。
ThingsDB
是 Thing(String what, String where)
的列表
列表片段:
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>
您在视图尚未膨胀时调用 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;
}
}
我收到以下错误:
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
我被困住了,希望你能帮忙。
ThingsDB
是 Thing(String what, String where)
列表片段:
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>
您在视图尚未膨胀时调用 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;
}
}