对话框列表适配器在nullPointerExeption上崩溃

时间:2015-12-09 10:58:35

标签: android arraylist android-arrayadapter android-alertdialog

我试图将列表视图调整为alertDialog,程序在dialog.setAdapter上崩溃并使用nullPointerExeption。

当用户点击列表视图中的项目时,应该弹出alertDialog。

ArrayAdapter<String> dialogAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userFunc);
    lv.setAdapter(dialogAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            if (position == 0) {


                AlertDialog.Builder alertDialog = new AlertDialog.Builder(ManagerFunctions.this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = (View) inflater.inflate(R.layout.user_op_list, null);
                alertDialog.setView(convertView);
                alertDialog.setTitle("Choose option");
                lv = (ListView) convertView.findViewById(R.id.lv);

                alertDialog.show();

此行是我收到错误的行:

        lv.setAdapter(dialogAdapter);

1 个答案:

答案 0 :(得分:0)

得到它,我需要将我的ArrayAdapter声明为final,因为它无法从内部类访问。

这样:

final String[] userFunc = new String[]{"חדש","מחק","ערוך"};
    final ArrayAdapter<String> dialogAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userFunc);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            if (position == 0) {


                AlertDialog.Builder alertDialog = new AlertDialog.Builder(ManagerFunctions.this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = (View) inflater.inflate(R.layout.user_op_list, null);
                alertDialog.setView(convertView);
                alertDialog.setTitle("מה ברצונך לעשות");
                lv = (ListView) convertView.findViewById(R.id.lv);
                lv.setAdapter(dialogAdapter);

                alertDialog.show();