游标适配器中的空指针异常

时间:2014-11-25 05:31:46

标签: android eclipse

public class PendingMessageFragment extends Fragment {

RegistrationAdapter adapter_ob;
RegistrationAdapter adapter_time;
RegistrationOpenHelper helper_ob;

ArrayList<MessageModel> pendingdata;
ListView pendinglist;
int j;
SQLiteDatabase db_ob;
ListView nameList;
Button registerBtn;
Cursor c;
Cursor cursor;
Bitmap bitmap;
TextView ptimeview;
ImageView postedimageviewrow;
StringBuffer now3;
String cool;
Time present;
@Override


public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View V = inflater.inflate(R.layout.pendingmessage, container, false);
    pendinglist=(ListView) V.findViewById(R.id.lv_name);
    pendingdata=new ArrayList<MessageModel>();
    adapter_ob = new RegistrationAdapter(getActivity());
    adapter_time=new RegistrationAdapter(getActivity());
    cursor=adapter_ob.queryName();
    Log.d("value of cursor in pendfrag is",""+cursor);
    cursor.moveToFirst();
    for(int i=0;i<cursor.getCount();i++)
    {
        MessageModel m=new MessageModel();
        m.setKey_Id(cursor.getString(cursor.getColumnIndex("_id")));
        m.setF_NAME(cursor.getString(cursor.getColumnIndex("F_NAME")));
        m.setF_Time(cursor.getString(cursor.getColumnIndex("F_Time")));
        pendingdata.add(m);
        cursor.moveToNext();
    }

    c=adapter_time.queryTime();
    Log.d("value of c befor onclick",""+c);
    pendinglist.setAdapter(new MyPendingAdapter(getActivity(),pendingdata));        
    pendinglist.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            Bundle passdata = new Bundle();
            Log.d("arg0 is",""+arg0);
            Log.d("arg2 is",""+arg2);

            //Here Getting arg0 and arg2 but in next line geeting null in listcursor.

            Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
            Log.d("listcursor is",""+listCursor);
            Log.d("column index is",""+j);
            int nameId = listCursor.getInt(listCursor
                    .getColumnIndex(helper_ob.KEY_ID));

            c=adapter_time.queryAll2(nameId);


                    Intent passIntent = new Intent(getActivity(),
                            EditActivity.class);
                    passIntent.putExtras(passdata);
                    startActivity(passIntent);



            }

            catch(ParseException e)
            {
                e.printStackTrace();
            }

        }
    });

    return V;

}
class MyPendingAdapter extends BaseAdapter
{ 
    Activity activity;
    ArrayList<MessageModel> arraylist;

    public MyPendingAdapter(Activity a,ArrayList<MessageModel> arr) {
        // TODO Auto-generated constructor stub
        this.activity=a;
        this.arraylist=arr;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        j=arraylist.size();
        return arraylist.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        View v=getActivity().getLayoutInflater().inflate(R.layout.row,null);

        arg0=j-arg0-1;
        TextView t1=(TextView) v.findViewById(R.id.tv_fname);
        TextView t2=(TextView) v.findViewById(R.id.ptime);
        ImageView v1=(ImageView) v.findViewById(R.id.pendingimageviewrow);


        bitmap=BitmapFactory.decodeResource(getActivity().getResources(),R.drawable.receviedpostedlogo);

        v1.setImageBitmap(bitmap);

        Log.d("pending message fragment time is",""+arraylist.get(arg0).getF_Time());
        t1.setText(arraylist.get(arg0).getF_NAME());
        t2.setText(arraylist.get(arg0).getF_Time());
        Log.d("the time is",""+arraylist.get(arg0).getF_Time());
        Log.d("t2 text is",""+t2.getText().toString());

        return v;
    }

}
@Override
public void onResume() {
    super.onResume();
    cursor.requery();

}

}

本程序正在从数据库中选择正确保存的数据,我在listview上显示数据,但是当点击任何项目时,它会在获取onclickitem数据时显示空指针异常。

日志

 11-25 11:42:48.779: D/arg0 is(1456): android.widget.ListView@40f646b0
 11-25 11:42:48.779: D/arg2 is(1456): 2
 11-25 11:42:48.779: D/listcursor is(1456): null
 11-25 11:42:48.789: D/column index is(1456): 3
 11-25 11:42:48.789: D/AndroidRuntime(1456): Shutting down VM
 11-25 11:42:48.789: W/dalvikvm(1456): threadid=1: thread exiting with uncaught exception (group=0x40c1fa68)
 11-25 11:42:48.799: E/AndroidRuntime(1456): FATAL EXCEPTION: main
 11-25 11:42:48.799: E/AndroidRuntime(1456): java.lang.NullPointerException
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at com.example.gate_keeper.PendingMessageFragment$1.onItemClick(PendingMessageFragment.java:98)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at android.widget.AbsListView.performItemClick(AbsListView.java:1394)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3024)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at android.widget.AbsListView$1.run(AbsListView.java:3830)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at android.os.Handler.handleCallback(Handler.java:605)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at android.os.Handler.dispatchMessage(Handler.java:92)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at android.os.Looper.loop(Looper.java:137)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at android.app.ActivityThread.main(ActivityThread.java:4517)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at java.lang.reflect.Method.invokeNative(Native Method)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at java.lang.reflect.Method.invoke(Method.java:511)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
 11-25 11:42:48.799: E/AndroidRuntime(1456):    at dalvik.system.NativeStart.main(Native Method)
 11-25 11:42:48.829: E/AlarmManagerService(378): android_server_AlarmManagerService_set to type=3, 116.173000000

2 个答案:

答案 0 :(得分:0)

尝试从列表数据持有者中获取选定的列表项数据:

int nameId = pendingdata.get(arg2).getKey_Id();

而不是:

int nameId = listCursor.getInt(listCursor.getColumnIndex(helper_ob.KEY_ID));

答案 1 :(得分:0)

使用

解决了问题

int nameId = pendingdata.get(arg2).getKey_Id();

而不是:

int nameId = listCursor.getInt(listCursor.getColumnIndex(helper_ob.KEY_ID));