日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了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);

這篇關于Android:嘗試在空對象引用上調用虛方法android.widget.ListView.setAdapter(android.widget.ListAdapter)&amp;#39;的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:amp Android 對象 嘗試 引用 方法 調用
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定