java - Android 中的 Dialog 内的 setAdapter() 无法弹出吗?

标签 java android dialog

我遇到了一个非常令人沮丧的问题。我有一个带有 onClick 事件的按钮,该按钮应该显示一个弹出对话框,其中包含 ExpandableListView。

这是代码:

public class Home extends Activity {

List<String> groupList;
List<String> childList;
Map<String, List<String>> laptopCollection;
ExpandableListView expListView; 

private Context thisContext = this; 

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    createGroupList();

    createCollection();


    Intent myDataIntent = this.getIntent();
    // getting the municipality name, the year and the versed money from the prev. intent
    this.municipalityName = myDataIntent.getStringExtra(TAG_COMUNE);
    this.year = myDataIntent.getStringExtra(TAG_ANNO);
    this.versedMoney = myDataIntent.getStringExtra(TAG_VERSED_MONEY);


    Button infoListButton = (Button) findViewById(R.id.infoButton);

    infoListButton.setOnClickListener(new OnClickListener() {

         @Override
          public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(thisContext);
            dialog.setContentView(R.layout.institutional_info_custom_list);
            dialog.setTitle("Info");

            LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
            dialog.setContentView(v);

            expListView = (ExpandableListView) findViewById(android.R.id.list);
                 final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
                        Home.this, groupList, laptopCollection);
            /// THE APP CRASHES HERE
            expListView.setAdapter(expListAdapter);

            // getting the window manager and changing the dialog position
            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
            params.gravity = Gravity.TOP | Gravity.LEFT;
            params.y = 80;
            dialog.getWindow().setAttributes(params);
            // dialog width and height.
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);         

            // set the custom dialog components

            dialog.show();
          }
    });
}


 private void createGroupList() {
        groupList = new ArrayList<String>();
        groupList.add("HP");
        groupList.add("Dell");
        groupList.add("Lenovo");
        groupList.add("Sony");
        groupList.add("HCL");
        groupList.add("Samsung");
    }

    private void createCollection() {
        // preparing laptops collection(child)
        String[] hpModels = { "HP Pavilion G6-2014TX", "ProBook HP 4540",
                "HP Envy 4-1025TX" };
        String[] hclModels = { "HCL S2101", "HCL L2102", "HCL V2002" };
        String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series",
                "ThinkPad X Series", "Ideapad Z Series" };
        String[] sonyModels = { "VAIO E Series", "VAIO Z Series",
                "VAIO S Series", "VAIO YB Series" };
        String[] dellModels = { "Inspiron", "Vostro", "XPS" };
        String[] samsungModels = { "NP Series", "Series 5", "SF Series" };

        laptopCollection = new LinkedHashMap<String, List<String>>();

        for (String laptop : groupList) {
            if (laptop.equals("HP")) {
                loadChild(hpModels);
            } else if (laptop.equals("Dell"))
                loadChild(dellModels);
            else if (laptop.equals("Sony"))
                loadChild(sonyModels);
            else if (laptop.equals("HCL"))
                loadChild(hclModels);
            else if (laptop.equals("Samsung"))
                loadChild(samsungModels);
            else
                loadChild(lenovoModels);

            laptopCollection.put(laptop, childList);
        }
    }

    private void loadChild(String[] laptopModels) {
        childList = new ArrayList<String>();
        for (String model : laptopModels)
            childList.add(model);
    }


}

这是 ExpandableListAdapter 类:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Activity context;
private Map<String, List<String>> laptopCollections;
private List<String> laptops;

public ExpandableListAdapter(Activity context, List<String> laptops,
        Map<String, List<String>> laptopCollections) {
    this.context = context;
    this.laptopCollections = laptopCollections;
    this.laptops = laptops;
}

public Object getChild(int groupPosition, int childPosition) {
    return laptopCollections.get(laptops.get(groupPosition)).get(childPosition);
}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final String laptop = (String) getChild(groupPosition, childPosition);
    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.institutional_rowlist_detail, null);
    }

    TextView item = (TextView) convertView.findViewById(R.id.institutional_detail_name);

    item.setText(laptop);
    return convertView;
}

public int getChildrenCount(int groupPosition) {
    return laptopCollections.get(laptops.get(groupPosition)).size();
}

public Object getGroup(int groupPosition) {
    return laptops.get(groupPosition);
}

public int getGroupCount() {
    return laptops.size();
}

public long getGroupId(int groupPosition) {
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String laptopName = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.institutional_rowlist_master,
                null);
    }
    TextView item = (TextView) convertView.findViewById(R.id.institutional_detail_name);
    item.setTypeface(null, Typeface.BOLD);
    item.setText(laptopName);
    return convertView;
}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

这是我正在使用的 4 个 .xml 文件(第一个是 Home Activity XML,名为 Activity_home.xml,其中包含按钮,第二个是带有 ExpandableListView 的文件,第三个是带有组的文件条目 TextView ,最后是详细信息):

1)activity_home.xml:

  <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/scrollView1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >

 <RelativeLayout
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="40dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="20dp" >

<Button
    android:id="@+id/investStatButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayout1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="@string/investStatButton" />

   </RelativeLayout>
    </ScrollView>

2)institutional_info_custom_list.xml

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
     android:layout_height="fill_parent" >

   <ExpandableListView
       android:id="@android:id/list"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_marginBottom="50dp"
       android:layout_marginLeft="50dp"
       android:layout_marginTop="30dp" >

</ExpandableListView>

3)institutional_rowlist_master.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/institutional_master_name"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginLeft="8dp"
android:gravity="left"
android:paddingLeft="32dp"
android:paddingTop="8dp"
android:textSize="14sp"
android:textAlignment="textEnd"
android:textStyle="bold" /> 

4)institutional_rowlist_detail.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/institutional_detail_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_launcher"
    android:drawablePadding="5dp"
    android:gravity="center_vertical"
    android:textSize="14sp"
    android:textStyle="bold" >
</TextView>

<TextView
    android:id="@+id/institutional_detail_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_launcher"
    android:drawablePadding="5dp"
    android:gravity="center_vertical"
    android:textSize="14sp"
    android:textStyle="bold" >
</TextView>

</LinearLayout> 

当我单击按钮时,应用程序崩溃,并且在 expListView.setAdapter(expListAdapter); 处收到 Java 空指针异常。主页 Activity 中的行。

请帮助我,因为我一整天都在网上寻找一些线索......我不知道该怎么办。

谢谢!

编辑:这是图像

enter image description here

编辑:谢谢codeMagic,我做到了: enter image description here

现在我想自定义信息标题栏,即添加一个带有 onclick 事件的按钮来关闭()对话框和一些 TextView 。是否可以???我怎样才能做到这一点?

最佳答案

现在,它正在寻找 activity_home.xml 内的 ExpandableListView,而它应该在您膨胀的 layout 中查找你的对话框。将其更改为

expListView = (ExpandableListView) v.findViewById(android.R.id.list);

编辑

您正在使用“@android:id/list”作为ListViewid。如果您的 Activity extends ListActivity 则应该使用此属性,否则您应该为其提供自己的 id 并使用它在 findViewById( )。无论哪种方式,您都需要查找正确的 layout 文件,正如我最初在答案中指出的那样。

关于java - Android 中的 Dialog 内的 setAdapter() 无法弹出吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431413/

相关文章:

dialog - Testcafe studio - 如何处理原生对话框

jsf - p :dialog is not calling controller method 内对话框中的按钮

android - GridView 画廊 |由标题分隔的图像

java - 将 csrf token 传递给客户端应用程序

java - JavaFX中是否有一个`strings.xml`文件?

android - 单击 span 和 div 中包含的文本

java - Android伪跨编码崩溃

java - JOptionPane.showInputDialog 始终返回 null

java - 如何编写 jsp 代码来接受来自文具店的订单并显示并包含用户可以选择和订购的商品列表

java - me.com 仅使用 javascript+css+html 编码?