Android:尝试在空对象引用上调用虚方法android.widget.ListView.setAdapter(android.widget.ListAdapter)'

我正在尝试创建一个包含另一个活动中的图片和文本的列表视图。我不太专注于Android编程,所以基本上就像Android编程的新手,我在调试我的应用程序时遇到了困难。请帮帮我:)

错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
  at com.fingervoiz.app.tamla.Activity.ListDataIdntActivity.onCreate(ListDataIdntActivity.java:53)

完整的日志目录:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fingervoiz.app.tamla, PID: 14968
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fingervoiz.app.tamla/com.fingervoiz.app.tamla.Activity.ListDataIdntActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
  at android.app.ActivityThread.access$800(ActivityThread.java:178)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
  at android.os.Handler.dispatchMessage(Handler.java:111)
  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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
  at com.fingervoiz.app.tamla.Activity.ListDataIdntActivity.onCreate(ListDataIdntActivity.java:53)
  at android.app.Activity.performCreate(Activity.java:6092)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608) 
  at android.app.ActivityThread.access$800(ActivityThread.java:178) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
  at android.os.Handler.dispatchMessage(Handler.java:111) 
  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 layoutResID;

    ArrayList<TamlaDataIdntModel> dataToList = new ArrayList<TamlaDataIdntModel>();

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        Holder holder = null;

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

            holder = new Holder();
            holder.txtFname = (TextView) view.findViewById(R.id.did_item_fullname);
            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 = (ImageView) view.findViewById(R.id.did_item_profilepic);
            view.setTag(holder);
        }else {
            holder = (Holder) view.getTag();
        }

        //FILL
        TamlaDataIdntModel dids = dataToList.get(position);
        if(dids != null){
            holder.txtFname.setText(dids.get_lname()+", "+dids.get_fname()+" "+dids.get_mname());
            holder.txtGender.setText(dids.get_gender());
            holder.txtBplace.setText(dids.get_bplace());
            holder.txtBday.setText(dids.get_bday());
            holder.txtAddress.setText(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{
        TextView txtFname;
        TextView txtGender;
        TextView txtBplace;
        TextView txtBday;
        TextView txtAddress;
        ImageView imgProfile;
    }
}

(列出活动)

public class ListDataIdntActivity  extends Activity{

    public static final String TAG = "ListDataIdntActivity";

    public static final int REQUEST_CODE_ADD_COMPANY = 40;
    public static final String EXTRA_ADDED_COMPANY = "extra_key_added_did";

    private ListView mListViewDID;
    private TextView emptyTxt;
    private Button b1;

    private ListDIDadapter adapter;
    private ArrayList<TamlaDataIdntModel> arryList;
    private DataIdntDAO mDIDdao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Initialize View
        mListViewDID = (ListView) findViewById(R.id.list_did);
        emptyTxt = (TextView) findViewById(R.id.txt_empty_list_dids);
        b1 = (Button) findViewById(R.id.back_list_did);

        //Fill List View
        mDIDdao = new DataIdntDAO(this);
        List<TamlaDataIdntModel> mIdntList = mDIDdao.getAllDID();

        if (mIdntList != null && !mIdntList.isEmpty()){
            adapter = new ListDIDadapter(this,R.layout.activity_list_did,arryList);
            mListViewDID.setAdapter(adapter);
        }else {
            emptyTxt.setVisibility(View.VISIBLE);
            mListViewDID.setVisibility(View.GONE);
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_CODE_ADD_COMPANY){
            if(requestCode == RESULT_OK){
                // add the added identity to the list and refresh the list view
                if(data != null){
                    TamlaDataIdntModel createdNewDID = (TamlaDataIdntModel)data.getSerializableExtra(EXTRA_ADDED_COMPANY);
                    if(createdNewDID != null){
                        if(arryList == null){
                            arryList = new ArrayList<TamlaDataIdntModel>();
                            arryList.add(createdNewDID);

                            if (adapter == null){
                                if (mListViewDID.getVisibility() != View.VISIBLE){
                                    mListViewDID.setVisibility(View.VISIBLE);
                                    emptyTxt.setVisibility(View.GONE);
                                }

                                adapter = new ListDIDadapter(this,R.layout.activity_list_did,arryList);
                                mListViewDID.setAdapter(adapter);
                            }else {

                                if (arryList != null){

                                    for (Object object : arryList) {

                                        adapter.insert((TamlaDataIdntModel) object, adapter.getCount());
                                    }
                                }
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }

                }
            }
        } else {
            super.onActivityResult(requestCode,resultCode,data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDIDdao.close();
    }
}

解决方案

您遗漏了一行非常重要的行-setContentView()。该行将XML与活动的视图绑定在一起,它是用于findViewById的视图。将此行添加到您的onCreate:

@Override
public void onCreate(Bundle state){
    super.onCreate(state);
    setContentView(R.layout.name_of_the_xml);

相关文章