3

試圖建立一個自定義適配器一個gridview,我讓我的光標,設置的AsyncTask適配器...NullPointerException異常與SimpleCursorAdapter

這裏是我的所有代碼:

private class getAllData extends AsyncTask<Context, Void, Cursor> { 
    protected void onPreExecute() { 
     dialog = ProgressDialog.show(Shows.this, "", 
       "Loading shows. Please wait...", true); 
    } 

    /** The system calls this to perform work in a worker thread and 
     * delivers it the parameters given to AsyncTask.execute() */ 

    @Override 
    protected Cursor doInBackground(Context... params) { 
     // TODO Auto-generated method stub 
     JSONParser.getAllData(params[0]); 
     c = mDbHelper.fetchAllShows(); 
     return c; 
    } 

    protected void onPostExecute(Cursor c) { 
     startManagingCursor(c); 

     String[] str = new String[] {ShowsDbAdapter.KEY_SHOW_TITLE}; 
     int[] to = new int[] {R.id.textView1}; 

     GridAdapter ga = new GridAdapter(Shows.this, R.layout.icon,c,str,to); 
     gridView.setAdapter(ga); 
     dialog.dismiss(); 
    } 
} 

而且這裏是我的適配器...

public class GridAdapter extends SimpleCursorAdapter { 
     private Context context; 
     private int mLayout; 
     private Cursor mCursor; 

     public GridAdapter(Context context, int layout, Cursor c, 
      String[] from, int[] to) { 
      super(context, layout, c, from, to); 

      this.context = context; 

      mLayout = layout; 

      this.mCursor = c; 


     } 



     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 

      Cursor c = getCursor(); 


      final LayoutInflater inflater = LayoutInflater.from(context); 

      View v = inflater.inflate(mLayout, null); 


      v.setLayoutParams(new GridView.LayoutParams(150,150)); 
      int nameCol = c.getColumnIndex("show_title"); 
      String name = c.getString(nameCol); 
      TextView tv = (TextView) v.findViewById(R.id.textView1); 

      if (name != null) { 
       Log.e("GRIDVIEW", "I'm in here"); 
       tv.setText(name); 
      } 

      return v; 

     } 


     public void bindView(View v, Context context, Cursor c) { 


      int nameCol = c.getColumnIndex("show_title"); 
      String name = c.getString(nameCol); 

      TextView tv = (TextView) v.findViewById(R.id.textView1); 
      if (name != null) { 
       tv.setText(name); 
      } 

      ImageView iv = (ImageView) v.findViewById(R.id.album_image); 
      iv.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      iv.setImageResource(R.drawable.icon); 
     } 

} 

我知道它有點亂,而不是最好的,但它應該是工作......這裏是我的錯誤:

> 07-05 11:02:32.729: ERROR/AndroidRuntime(5953): FATAL EXCEPTION: main 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953): java.lang.NullPointerException 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at com.android.showapp.Shows$getAllData.onPostExecute(Shows.java:112) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at com.android.showapp.Shows$getAllData.onPostExecute(Shows.java:1) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at android.os.AsyncTask.finish(AsyncTask.java:417) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at android.os.AsyncTask.access$300(AsyncTask.java:127) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at android.os.Looper.loop(Looper.java:144) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-05 11:02:32.729: ERROR/AndroidRuntime(5953):  at dalvik.system.NativeStart.main(Native Method) 

R.layout.icon does exist (and it holds the textview and imageview) 
+3

的logcat的輸出參考上下文和光標顯示在你的代碼(112)線。上面哪條線是?你初始化gridView嗎? – thaussma

+1

您不需要對適配器中的上下文和光標的引用。 – thaussma

+0

對Herrmann的建議+1和對某人發佈堆棧跟蹤的例外+1。 –

回答

0

答案是從赫爾曼 - 沒有必要在我的適配器

0

您正在嘗試通過光標訪問記錄,然後指向記錄。失敗的代碼很可能是您的newView覆蓋中此代碼段的第二行。

int nameCol = c.getColumnIndex("show_title"); 
    String name = c.getString(nameCol); 
    TextView tv = (TextView) v.findViewById(R.id.textView1); 

    if (name != null) { 
     Log.e("GRIDVIEW", "I'm in here"); 
     tv.setText(name); 
    } 

您不能嘗試訪問除bindView以外的遊標的字段值。

您應該能夠在適配器的contstructor中獲得列索引,並將它們存儲爲字段以供將來在bindView中使用。

newView應該只用於創建一個視圖。