我的代码无法找到带有android: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
}
});
}
和gridview:
<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)
答案 0 :(得分:0)
确保您没有导入android.R(Eclipse会建议这样做)。您应该导入包的.R
答案 1 :(得分:0)
GridView grid = (GridView) ViewAlbum.this.findViewById(com.example.whs.R.id.grid);
你不会以这种方式获得视图。
如果您的异步任务是同一活动中的内部类,则调用get like this
GridView grid = (GridView) getActivity().findViewById(com.example.whs.R.id.grid);
如果您的异步任务位于单独的类中,则需要对视图进行充气以访问gridview