无法在Dialog弹出Android中设置Adapter()?

时间:2014-03-16 00:15:20

标签: 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活动XML,名为activity_home.xml,其中包含按钮,第二个是具有ExpandableListView的文件,第三个是具有该组的文件)输入文本视图,最后一个是详细信息):

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 Null Pointer异常;在家庭活动中排队。

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

谢谢!

编辑:这是图片

enter image description here

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

现在我想自定义信息标题栏,即添加一个带有onclick事件的按钮来关闭()对话框和一些文本视图。可能吗???我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

现在,它正在查找ExpandableListView内的activity_home.xml,它应该在layout中查找您为Dialog充气的内容。将其更改为

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

修改

您正在"@android:id/list"使用id ListView。如果您Activity extends ListActivity,则应该使用此方法,否则您应该将其设为自己的id,并使用该findViewById()来访问它。无论哪种方式,您都需要查找正确的layout文件,正如我在回答中最初提到的那样。