自定义gridview,onItemClickListener返回错误

时间:2013-10-22 19:18:22

标签: android gridview actionbarsherlock onitemclicklistener custom-adapter

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

所有帮助表示感谢。

2 个答案:

答案 0 :(得分:2)

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

尝试:

mContext = this;

或者也许     mContext = getActivity(); //片段

OnCreateView()中的某个地方

答案 1 :(得分:0)

你的行

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

应该是

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

我认为缺少的开幕式正在破坏你的应用