2016-07-10 6 views
0

私のリストビューに削除機能を作成しようとしていますが、問題が見つけられず、私は問題が他のアクティビティでokを実行している行のポインタであるため、どこに問題があるのか​​分かりません。Android:ヌルオブジェクトリファレンスで 'android.widget.TextViewフィールドを読み込もうとしています

**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) 

リストビューアダプタで私のコード

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の活動nullを返す

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; 
      } 
     }); 

    } 
} 

答えて

8

ListDIDadapter $ Holder.txtFname」nullオブジェクト参照の

このラインにより

holder = (Holder) view.getTag(); 

view.getTag()ですview.setTag(holder)は呼び出されませんif-block

+0

サー、私の問題を解決するには、私のコードでview.setTag(ホルダー)を追加することです? :)前もって感謝 – gunZ333

+0

@ gunZ333: 'holder.imgProfile =(CircleImageView)view.findViewById(R.id.did_item_profilepic);'行追加 'view.setTag(ホルダー);'の後にはいいいえ;行 –

+0

このコード行を追加してください。 – gunZ333

関連する問題