0
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);
} 

感谢所有帮助。

4

2 回答 2

2

您已将 mContext 列为变量,但从未设置过它。

尝试 :

mContext = this;

或者也许 mContext = getActivity(); // 对于一个片段

OnCreateView() 中的某处

于 2013-10-22T19:40:05.707 回答
0

你的线

static final String[] mobile_os = "lots and lots of strings here"
};

应该

static final String[] mobile_os = { "lots and lots of strings here"
};

我认为缺少的左括号是破坏您的应用程序的原因

于 2013-10-22T19:37:39.020 回答