【问题标题】:Error on iniatialisation of Dialog box elements对话框元素初始化错误
【发布时间】:2020-05-12 11:18:23
【问题描述】:

我正在处理一个android项目,我需要使用数组数据在ListView上显示数据现在我的对话框正在工作,但是当我定义列表视图时,会显示一个错误:

进程:com.example.uhf,PID:31014

java.lang.NullPointerException: 尝试调用虚方法

'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' 在空对象引用上

目前,我认为问题在于查找 LV id,因为我使用 getView 功能,所以大家可以帮我解决它吗?

我的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
ViewGroup viewGroup = getView().findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.custom_dialog_display_misssing_data, viewGroup, false);
builder.setView(dialogView);
AlertDialog alertDialog = builder.create();
ListView listViewDialog=(ListView)getView().findViewById(R.id.DialogLV);
String[] values = new String[] { "Android List View",
                                 "Adapter implementation",
                                 "Simple List View In Android",
                                 "Create List View Android",
                                 "Android Example",
                                 "List View Source Code",
                                 "List View Array Adapter",
                                 "Android Example List View"
                               };
ArrayAdapter<String> adapterLVDialog = new ArrayAdapter<String>(getContext(),
                                         android.R.layout.simple_list_item_1,
                                         android.R.id.text1, values);                                        
listViewDialog.setAdapter(adapterLVDialog);
alertDialog.show();

custom_dialog_display_missing_data.xml:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This List containts all missing tags elements"
    android:textAlignment="center"
/>
<ListView
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:id="@+id/DialogLV">
</ListView>

【问题讨论】:

    标签: java android listview dialog


    【解决方案1】:

    进程:com.example.uhf,PID:31014 java.lang.NullPointerException: 尝试调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' 空对象引用

    不要

     ListView listViewDialog=(ListView)getView().findViewById(R.id.DialogLV);
    

     ListView listViewDialog=(ListView)dialogView.findViewById(R.id.DialogLV);
    

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 2014-02-10
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多