我的代码找不到 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)