NullPointerException异常与SimpleCursorAdapter [英] NullPointerException with SimpleCursorAdapter
本文介绍了NullPointerException异常与SimpleCursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图建立一个自定义适配器一个gridview,我得到我的光标,并设置在AsyncTask的...
适配器下面是我的code的一切:
私有类getAllData扩展的AsyncTask<上下文,太虚,光标> {
在preExecute保护无效(){
对话框= ProgressDialog.show(Shows.this,,
加载显示请稍候...,真正的);
} / **系统调用这在辅助线程执行工作,
*它传递给AsyncTask.execute()的参数* / @覆盖
保护光标doInBackground(上下文... PARAMS){
// TODO自动生成方法存根
JSONParser.getAllData(PARAMS [0]);
C = mDbHelper.fetchAllShows();
返回℃;
} 保护无效onPostExecute(光标C){
startManagingCursor(C); 的String []海峡=新的String [] {} ShowsDbAdapter.KEY_SHOW_TITLE;
INT []为= INT新[] {} R.id.textView1; GridAdapter GA =新GridAdapter(Shows.this,R.layout.icon,C,STR,至);
gridView.setAdapter(GA);
dialog.dismiss();
}
}
这是我的适配器...
公共类GridAdapter扩展SimpleCursorAdapter {
私人上下文的背景下;
私人诠释mLayout;
私人光标mCursor; 公共GridAdapter(上下文的背景下,INT布局,光标C,
的String []从,INT []到){
超(背景下,布局,C,从,到); this.context =背景; mLayout =布局; this.mCursor = C;
} @覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){ 光标C = getCursor();
最后LayoutInflater吹气= LayoutInflater.from(背景); 视图V = inflater.inflate(mLayout,NULL);
v.setLayoutParams(新GridView.LayoutParams(150,150));
INT nameCol = c.getColumnIndex(SHOW_TITLE);
字符串名称= c.getString(nameCol);
TextView的电视=(TextView中)v.findViewById(R.id.textView1); 如果(名字!= NULL){
Log.e(GRIDVIEW,我在这里);
tv.setText(名);
} 返回伏; }
公共无效bindView(视图V,上下文的背景下,光标C){
INT nameCol = c.getColumnIndex(SHOW_TITLE);
字符串名称= c.getString(nameCol); TextView的电视=(TextView中)v.findViewById(R.id.textView1);
如果(名字!= NULL){
tv.setText(名);
} ImageView的IV =(ImageView的)v.findViewById(R.id.album_image);
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setImageResource(R.drawable.icon);
}}
我意识到它的有点乱,而不是最好的,但它应该是工作......这里是我的错误:
> 11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):致命异常:主要
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):显示java.lang.NullPointerException
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在com.android.showapp.Shows $ getAllData.onPostExecute(Shows.java:112)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在com.android.showapp.Shows $ getAllData.onPostExecute(Shows.java:1)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在android.os.AsyncTask.finish(AsyncTask.java:417)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在android.os.AsyncTask.access $ 300(AsyncTask.java:127)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:429)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在android.os.Handler.dispatchMessage(Handler.java:99)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在android.os.Looper.loop(Looper.java:144)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在android.app.ActivityThread.main(ActivityThread.java:4937)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在java.lang.reflect.Method.invokeNative(本机方法)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在java.lang.reflect.Method.invoke(Method.java:521)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11月7日至5日:02:32.729:ERROR / AndroidRuntime(5953):在dalvik.system.NativeStart.main(本机方法)R.layout.icon确实存在(它持有的TextView和ImageView的)
解决方案
答来自赫尔曼 - 没有必要对我的适配器参考上下文和光标
Trying to set up a gridview with a custom adapter, I get my cursor and set the adapter in ASyncTask...
Here is my code for it all:
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();
}
}
And here is my Adapter...
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);
}
}
I realize its a little messy and not the best but it should be working... here are my errors:
> 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)
解决方案
Answer was from Herrmann - no need for a reference to the context and the cursor in my adapter
这篇关于NullPointerException异常与SimpleCursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文