Android:尝试从空对象引用

时间:2016-07-10 07:03:09

标签: android listview null delete-row

我正在尝试为我的listview创建一个删除功能,但有一些问题,我无法找到,甚至我发现logcat中的问题,我不知道问题在哪里,因为问题是指针在一行在其他活动中运行正常。请帮助我,并提前感谢:)

以下是错误指向:

**07-10 14:49:48.705 25554-25554/com.fingervoiz.app.tamla E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fingervoiz.app.tamla, PID: 25554
java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.fingervoiz.app.tamla.Adapter.ListDIDadapter$Holder.txtFname' on a null object reference
 at com.fingervoiz.app.tamla.Adapter.ListDIDadapter.getView(ListDIDadapter.java:61)**


 at android.widget.AbsListView.obtainView(AbsListView.java:2467)
 at android.widget.ListView.makeAndAddView(ListView.java:1894)
 at android.widget.ListView.fillSpecific(ListView.java:1360)
 at android.widget.ListView.layoutChildren(ListView.java:1673)
 at android.widget.AbsListView.onLayout(AbsListView.java:2230)
 at android.view.View.layout(View.java:16001)
 at android.view.ViewGroup.layout(ViewGroup.java:5181)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
 at android.view.View.layout(View.java:16001)
 at android.view.ViewGroup.layout(ViewGroup.java:5181)
 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
 at android.view.View.layout(View.java:16001)
 at android.view.ViewGroup.layout(ViewGroup.java:5181)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
 at android.view.View.layout(View.java:16001)
 at android.view.ViewGroup.layout(ViewGroup.java:5181)
 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
 at android.view.View.layout(View.java:16001)
 at android.view.ViewGroup.layout(ViewGroup.java:5181)
 at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2492)
 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2189)
 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1301)
 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6673)
 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
 at android.view.Choreographer.doCallbacks(Choreographer.java:603)
 at android.view.Choreographer.doFrame(Choreographer.java:572)
 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
 at android.os.Handler.handleCallback(Handler.java:815)
 at android.os.Handler.dispatchMessage(Handler.java:104)
 at android.os.Looper.loop(Looper.java:194)
 at android.app.ActivityThread.main(ActivityThread.java:5637)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

我在listview适配器中的代码

public class ListDIDadapter extends ArrayAdapter<TamlaDataIdntModel> {

    Context context;
    int layoutResourceId;
    ArrayList<TamlaDataIdntModel> data = new ArrayList<TamlaDataIdntModel>();

    public ListDIDadapter (Context context,int layoutResourceId,ArrayList<TamlaDataIdntModel> data){
        super(context,layoutResourceId,data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        Holder holder;

        if (view == null){
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            view = inflater.inflate(layoutResourceId,parent,false);

            holder = new Holder();
            holder.txtFname = (TextView) view.findViewById(R.id.did_item_fname);
            holder.txtMname = (TextView) view.findViewById(R.id.did_item_mname);
            holder.txtLname = (TextView) view.findViewById(R.id.did_item_lname);
            holder.txtGender = (TextView) view.findViewById(R.id.did_item_gender);
            holder.txtBplace = (TextView) view.findViewById(R.id.did_item_bplace);
            holder.txtBday = (TextView) view.findViewById(R.id.did_item_bday);
            holder.txtAddress = (TextView) view.findViewById(R.id.did_item_address);
            holder.imgProfile = (CircleImageView) view.findViewById(R.id.did_item_profilepic);

        }else {
            holder = (Holder) view.getTag();
        }

        //Feed Data
        TamlaDataIdntModel dids = data.get(position);
        holder.txtFname.setText("GIVEN NAME: "+dids.get_fname());
        holder.txtMname.setText("MIDDLE NAME: "+dids.get_mname());
        holder.txtLname.setText("FAMILY NAME: "+dids.get_lname());
        holder.txtGender.setText("Gender: "+dids.get_gender());
        holder.txtBplace.setText("Place of Birth: "+dids.get_bplace());
        holder.txtBday.setText("Birthday: "+dids.get_bday());
        holder.txtAddress.setText("Address: "+dids.get_address());

        byte[] outImage = dids.get_dp();
        ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
        Bitmap theImage = BitmapFactory.decodeStream(imageStream);
        holder.imgProfile.setImageBitmap(theImage);

        return view;
    }

    static class Holder{
        CircleImageView imgProfile;
        TextView txtFname,txtMname,txtLname,txtGender,txtBplace,txtBday,txtAddress;
    }
}
删除功能中的

代码

/*DELETE DATA IDENTITY*/
    public void deleteDID(long id){

        SQLiteDatabase db = this.mDBHelper.getWritableDatabase();
        db.delete(TamlaDatabaseHelper.TABLE_DATAIDNT,TamlaDatabaseHelper.COLUMN_DI_ID + " = ?",
                new String[]{String.valueOf(id)});
        db.close();
    }

,这是ListView活动

public class ListDataIdntActivity extends Activity{

    ArrayList<TamlaDataIdntModel> imgArray = new ArrayList<TamlaDataIdntModel>();
    ListDIDadapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_did);

        DataIdntDAO db = new DataIdntDAO(this);
        List<TamlaDataIdntModel> inData = db.getAllDID();

        for (TamlaDataIdntModel cn : inData){
            imgArray.add(cn);
        }

        /*BACK BUTTON*/
        Button back = (Button)findViewById(R.id.back_list_did);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        /*LIST ADAPTER*/
        adapter = new ListDIDadapter(this,R.layout.list_item_did_details,imgArray);
        final ListView dataList = (ListView) findViewById(R.id.list_did);
        dataList.setAdapter(adapter);

        /*LIST ADAPTER ITEM LONG CLICK*/
        dataList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                final TamlaDataIdntModel num = (TamlaDataIdntModel) parent.getItemAtPosition(position);

                final CharSequence[] items = {"EDIT","DELETE","CANCEL"};
                AlertDialog.Builder builder = new AlertDialog.Builder(ListDataIdntActivity.this);
                builder.setTitle("YOUR CHOICE");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {

                        if (items[item].equals("EDIT")) {

                            /*Intent pass = new Intent(ListDataIdntActivity.this, UserEdit.class);
                            pass.putExtra("Edit_Ses", idd);
                            startActivity(pass);*/

                        } else if (items[item].equals("DELETE")) {

                            DataIdntDAO db = new DataIdntDAO(ListDataIdntActivity.this);
                            db.deleteDID(position);
                            adapter.notifyDataSetChanged();

                        } else if (items[item].equals("CANCEL")) {
                            dialog.dismiss();
                        }
                    }
                });
                builder.show();
                return true;
            }
        });

    }
}

1 个答案:

答案 0 :(得分:11)

  

ListDIDadapter $ Holder.txtFname&#39;在空对象引用上

由于这一行

holder = (Holder) view.getTag();

view.getTag()返回null,因为view.setTag(holder)未调用if-block