CursorAdapter上的空指针异常

时间:2014-12-23 05:36:57

标签: java android nullpointerexception

我得到NullPointerException。您可以在下面找到我的Logcat和相关代码。

logcat的:

12-23 00:17:35.330: E/AndroidRuntime(2019): FATAL EXCEPTION: main
12-23 00:17:35.330: E/AndroidRuntime(2019): Process: com.android.timesheet, PID: 2019
12-23 00:17:35.330: E/AndroidRuntime(2019): java.lang.NullPointerException
12-23 00:17:35.330: E/AndroidRuntime(2019):     at com.android.timesheet.adapter.CustomCursorAdapter$1.onClick(CustomCursorAdapter.java:54)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at android.view.View.performClick(View.java:4438)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at android.view.View$PerformClick.run(View.java:18422)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at android.os.Handler.handleCallback(Handler.java:733)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at android.os.Looper.loop(Looper.java:136)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at java.lang.reflect.Method.invoke(Method.java:515)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-23 00:17:35.330: E/AndroidRuntime(2019):     at dalvik.system.NativeStart.main(Native Method)

CustomCursorAdapter.java:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.android.timesheet.ModifyMember;
import com.android.timesheet.R;

public class CustomCursorAdapter extends CursorAdapter {

    Button delete_btn;
    TextView memID_tv, memName_tv;
    @SuppressWarnings("deprecation")
    public CustomCursorAdapter(Activity context, Cursor c) {
        super(context, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // when the view will be created for first time,
        // we need to tell the adapters, how each item will look
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.single_row_item, parent, false);

        return retView;
    }

    @Override
    public void bindView(View view, final Context context, Cursor cursor) {
        // here we are setting our data
        // that means, take the data from the cursor and put it in views 

        TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
        textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
        delete_btn=(Button)view.findViewById(R.id.delete_btn);

        delete_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                memID_tv = (TextView)v.findViewById(R.id.member_id);
                memName_tv = (TextView)v.findViewById(R.id.member_name);

                String memberID_val = memID_tv.getText().toString();;  ---->54th Line
                String memberName_val = memName_tv.getText().toString();

                Intent i = new Intent(context,
                        ModifyMember.class);
                i.putExtra("memberName", memberName_val);
                i.putExtra("memberID", memberID_val);
                ((Activity)context).startActivity(i);

            }  
        });  

    }  
}

我正在使用删除按钮删除所有listview行项目。那时我得到了NullPointerException

3 个答案:

答案 0 :(得分:0)

使用view(行视图)代替v方法的onClick参数,该方法是Button的视图:

        delete_btn=(Button)view.findViewById(R.id.delete_btn);
        delete_btn.setTag(view);
        delete_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                View rowview=(View)v.getTag();
                memID_tv = (TextView)rowview.findViewById(R.id.member_id);
                memName_tv = (TextView)rowview.findViewById(R.id.member_name);

            }  
        });  

答案 1 :(得分:0)

使用view代替v

memID_tv = (TextView)view.findViewById(R.id.member_id);

在bindview中试试这个 -

if (view == null) {
            view = inflater.inflate(R.layout.single_row_item, parent, null);
}
 TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
        textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
        delete_btn=(Button)view.findViewById(R.id.delete_btn);
.
.
.

答案 2 :(得分:0)

您将从单击视图中获取视图,但您必须从根视图中获取视图。

                memID_tv = (TextView)v. findViewById(R.id.member_id);
                memName_tv = (TextView)v.findViewById(R.id.member_name);

将这些行改为:

                memID_tv = (TextView)view. findViewById(R.id.member_id);
                memName_tv = (TextView)view.findViewById(R.id.member_name);