片段中的错误客户 gridview
error custome gridview in the fragment
我有一个要显示自定义 gridview 的片段。 gridview 工作正常但是当我把它放在片段中时它的力关闭。
这是我的代码:
片段:
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
int[] icons = { R.drawable.image1, R.drawable.image2, R.drawable.image5 };
String[] names = { "asd ", "Asdasd", "asd " };
CustomGrid grid = new CustomGrid(getActivity().getApplicationContext(), names, icons);
GridView g = (GridView) getActivity().findViewById(R.id.gridView1);
g.setAdapter(grid);
return rootView;
}
这是我强制关闭时的 adb 日志:
01-04 08:30:22.899: E/AndroidRuntime(2379): FATAL EXCEPTION: main
01-04 08:30:22.899: E/AndroidRuntime(2379): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.tashrifat/com.plusnet.tashrifat.MainActivity}: java.lang.NullPointerException
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.access0(ActivityThread.java:141)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.os.Looper.loop(Looper.java:137)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-04 08:30:22.899: E/AndroidRuntime(2379): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 08:30:22.899: E/AndroidRuntime(2379): at java.lang.reflect.Method.invoke(Method.java:525)
01-04 08:30:22.899: E/AndroidRuntime(2379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-04 08:30:22.899: E/AndroidRuntime(2379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-04 08:30:22.899: E/AndroidRuntime(2379): at dalvik.system.NativeStart.main(Native Method)
01-04 08:30:22.899: E/AndroidRuntime(2379): Caused by: java.lang.NullPointerException
01-04 08:30:22.899: E/AndroidRuntime(2379): at com.plusnet.tashrifat.HomeFragment.onCreateView(HomeFragment.java:28)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.Fragment.performCreateView(Fragment.java:1695)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.BackStackRecord.run(BackStackRecord.java:682)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.Activity.performStart(Activity.java:5142)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
01-04 08:30:22.899: E/AndroidRuntime(2379): ... 11 more
使用 rootView
而不是 getActvity()
初始化 GridView,因为 GridView 存在于 fragment_home
布局中,即当前片段布局:
GridView g = (GridView) rootView.findViewById(R.id.gridView1);
我有一个要显示自定义 gridview 的片段。 gridview 工作正常但是当我把它放在片段中时它的力关闭。 这是我的代码:
片段:
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
int[] icons = { R.drawable.image1, R.drawable.image2, R.drawable.image5 };
String[] names = { "asd ", "Asdasd", "asd " };
CustomGrid grid = new CustomGrid(getActivity().getApplicationContext(), names, icons);
GridView g = (GridView) getActivity().findViewById(R.id.gridView1);
g.setAdapter(grid);
return rootView;
}
这是我强制关闭时的 adb 日志:
01-04 08:30:22.899: E/AndroidRuntime(2379): FATAL EXCEPTION: main
01-04 08:30:22.899: E/AndroidRuntime(2379): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.tashrifat/com.plusnet.tashrifat.MainActivity}: java.lang.NullPointerException
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.access0(ActivityThread.java:141)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.os.Looper.loop(Looper.java:137)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-04 08:30:22.899: E/AndroidRuntime(2379): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 08:30:22.899: E/AndroidRuntime(2379): at java.lang.reflect.Method.invoke(Method.java:525)
01-04 08:30:22.899: E/AndroidRuntime(2379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-04 08:30:22.899: E/AndroidRuntime(2379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-04 08:30:22.899: E/AndroidRuntime(2379): at dalvik.system.NativeStart.main(Native Method)
01-04 08:30:22.899: E/AndroidRuntime(2379): Caused by: java.lang.NullPointerException
01-04 08:30:22.899: E/AndroidRuntime(2379): at com.plusnet.tashrifat.HomeFragment.onCreateView(HomeFragment.java:28)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.Fragment.performCreateView(Fragment.java:1695)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.BackStackRecord.run(BackStackRecord.java:682)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.Activity.performStart(Activity.java:5142)
01-04 08:30:22.899: E/AndroidRuntime(2379): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
01-04 08:30:22.899: E/AndroidRuntime(2379): ... 11 more
使用 rootView
而不是 getActvity()
初始化 GridView,因为 GridView 存在于 fragment_home
布局中,即当前片段布局:
GridView g = (GridView) rootView.findViewById(R.id.gridView1);