0

我的代码找不到 GridViewandroid:id="@+id/grid"

找到我使用的gridview:GridView grid = (GridView) ViewAlbum.this.findViewById(R.id.grid);

在一个ASyncTask我必须做adapter一个mysql表。Mysql工作正常,但是当我试图找到gridview.

这是onPostExecute部分:

   protected void onPostExecute(String result){
            // dismiss dialog
            pDialog.dismiss();      
            // Add adapter to the list
            ViewAlbumAdapter adapter = new ViewAlbumAdapter(ViewAlbum.this, photoList);
            GridView grid = (GridView) ViewAlbum.this.findViewById(com.example.whs.R.id.grid);
            grid.setAdapter(adapter);
            grid.setOnItemClickListener(new OnItemClickListener(){
                public void onItemClick(AdapterView<?> a, View v,
                        int position, long id) {
                // do something
                }
            });
        }

和网格视图:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ViewAlbum" >

    <GridView android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="50dp"
        android:horizontalSpacing="5dp"
        />   

</RelativeLayout>

这是错误的日志:

03-02 18:07:43.545: E/AndroidRuntime(16432): FATAL EXCEPTION: main
03-02 18:07:43.545: E/AndroidRuntime(16432): java.lang.NullPointerException
03-02 18:07:43.545: E/AndroidRuntime(16432):    at com.example.whs.ViewAlbum$LoadAllItems.onPostExecute(ViewAlbum.java:139)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at com.example.whs.ViewAlbum$LoadAllItems.onPostExecute(ViewAlbum.java:1)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at android.os.AsyncTask.finish(AsyncTask.java:631)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at android.os.Looper.loop(Looper.java:137)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at java.lang.reflect.Method.invokeNative(Native Method)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at java.lang.reflect.Method.invoke(Method.java:511)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-02 18:07:43.545: E/AndroidRuntime(16432):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

确保您没有导入 android.R(Eclipse 会建议这样做)。你应该导入你的包的 .R

于 2013-03-02T14:12:01.993 回答
0
GridView grid = (GridView) ViewAlbum.this.findViewById(com.example.whs.R.id.grid);

您不会以这种方式获得视图。

如果您的异步任务是同一活动中的内部类,则像这样调用 get

GridView grid = (GridView) getActivity().findViewById(com.example.whs.R.id.grid);

如果您的异步任务在单独的类中,则需要扩展视图以访问您的网格视图

于 2013-03-02T16:48:20.783 回答