E/AndroidRuntime(8477): FATAL EXCEPTION: main
E/AndroidRuntime(8477): java.lang.NullPointerException
E/AndroidRuntime(8477): at android.content.ComponentName.<init>(ComponentName.java:75)
E/AndroidRuntime(8477): at android.content.Intent.<init>(Intent.java:3518)
E/AndroidRuntime(8477): at com.package.name.library.AllTab$1.onItemClick(AllTab.java:66)
E/AndroidRuntime(8477): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
E/AndroidRuntime(8477): at android.widget.AbsListView.performItemClick(AbsListView.java:1102)
E/AndroidRuntime(8477): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2751)
E/AndroidRuntime(8477): at android.widget.AbsListView$1.run(AbsListView.java:3426)
E/AndroidRuntime(8477): at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(8477): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(8477): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(8477): at android.app.ActivityThread.main(ActivityThread.java:5229)
E/AndroidRuntime(8477): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(8477): at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(8477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
E/AndroidRuntime(8477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
E/AndroidRuntime(8477): at dalvik.system.NativeStart.main(Native Method)
gridview 每个项目都有一个图像和一行文本。错误在 onItemClick 这里,我设置了一个测试布局和类,看看它是否会在那里发送信息。
无法弄清楚为什么会抛出此错误,因为我很确定我有正确的做法。这是我的代码,是否与使用 SherlockFragment 有关?:
public class AllTab extends SherlockFragment {
GridView gridView;
TextView itemLabel;
Context mContext;
static final String[] mobile_os = new String[] {
lots of string here };
@Override
public SherlockFragmentActivity getSherlockActivity() {
return super.getSherlockActivity();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaceState) {
View view = inflater.inflate(R.layout.gridview_alltab, container, false);
gridView = (GridView) view.findViewById(R.id.AllTabGridView);
itemLabel = (TextView) view.findViewById(R.id.item_label);
gridView.setAdapter(new AllTabImageAdapter(getSherlockActivity(), mobile_os));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
switch(position) {
case 0:
Intent one = new Intent(mContext, Test.class);
one.putExtra("databaseUrl", "SELECT * FROM stuffs");
startActivity(one);
break;
}
}
});
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
感谢所有帮助。