我有以下片段类:
public class Home extends Fragment {
public static final String TAG = Home.class.getSimpleName();
public static HomenewInstance() {
return new Home();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.Homegrag, container, false);
return v;
}
}
当我使用它时,它是工作perferct。但是当我在这个类中添加下面的代码时,它会给我错误:
public class Home extends Fragment {
private List<RowItem> rowItems;
private static Integer[] images = { R.drawable.red, R.drawable.red,
R.drawable.red, R.drawable.red, R.drawable.red, R.drawable.red,
R.drawable.red, R.drawable.red };
public static final String TAG = Home.class.getSimpleName();
public static HomenewInstance() {
return new Home();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.Homegrag, container, false);
ListView lv = (ListView) getActivity().findViewById(R.id.myList);
rowItems = new ArrayList<RowItem>();
String[] titles = { "Movie1", "Movie2", "Movie3", "Movie4", "Movie5",
"Movie6", "Movie7", "Movie8" };
String[] descriptions = { "First Movie", "Second movie", "Third Movie",
"Fourth Movie", "Fifth Movie", "Sixth Movie", "Seventh Movie",
"Eighth Movie" };
// Populate the List
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
// Set the adapter on the ListView
LazyAdapter adapter = new LazyAdapter(getActivity()
.getApplicationContext(), R.layout.list_row, rowItems);
lv.setAdapter(adapter);
lv.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return v;
}
}
然后它会在lv.setAdapter(adapter);
Logcat:
FATAL EXCEPTION: main
Process: com.paulusworld.drawernavigationtabs, PID: 11867
java.lang.NullPointerException
at com.paulusworld.drawernavigationtabs.Rush.onCreateView(Rush.java:55)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
我该怎么做?我想用lazyadapter绑定列表。
答案 0 :(得分:3)
更改此
ListView lv = (ListView) getActivity().findViewById(R.id.myList);
到
ListView lv = (ListView) v.findViewById(R.id.myList);
getActivity()
表示Fragment
的当前上下文,而返回的视图表示初始化View
的参考。
答案 1 :(得分:2)
尝试以下代码,如果有帮助请告诉我
LazyAdapter adapter = new LazyAdapter(getActivity(),R.layout.list_row, rowItems);
lv.setAdapter(adapter);
并确保lv不为空,以防万一...
并使用以下代码
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ListView) view.findViewById(R.id. myList);
lv.setAdapter(adapter);
}