0

我正在使用 xml 文件执行动态 ExpandableListView,但是当我加载到列表中没有内容的布局时崩溃了。以下是我的代码:

PasswordListingActivity.java:

public class PasswordListingActivity extends ExpandableListActivity {
    Button b_addPwd;
    ExpandableListView elv_pwdList;

    List<Map<String, String>> ServiceList = new ArrayList<Map<String, String>>();
    List<List<Map<String, String>>> DetailList = new ArrayList<List<Map<String, String>>>();

    ExpandableListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.passwordlisting);        

        b_addPwd = (Button)findViewById(R.id.b_addpwd);
        elv_pwdList = (ExpandableListView)findViewById(R.id.password_list);

        b_addPwd.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setClass(PasswordListingActivity.this, AddPasswordActivity.class);
                startActivityForResult(intent, 0);
            }           
    });

    adapter = new SimpleExpandableListAdapter(
            this, 
            ServiceList, 
            android.R.layout.simple_expandable_list_item_1, 
            new String[] {"SERVICE"}, 
            null, 
            DetailList, 
            android.R.layout.simple_expandable_list_item_2, 
            new String[] {"TITLE", "CONTENT",}, 
            null
    );
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (resultCode){
        case RESULT_OK:
            Bundle bundleR = data.getExtras();
            Map<String, String> ServiceGroupMap = new HashMap<String, String>();
            ServiceGroupMap.put("SERVICE", bundleR.getString("service"));
            ServiceList.add(ServiceGroupMap);
            List<Map<String, String>> DetailGroup = new ArrayList<Map<String, String>>();
            Map<String, String> DetailGroupMap = new HashMap<String, String>();
            DetailGroupMap.put("TITLE", "User Name: ");
            DetailGroupMap.put("CONTENT", bundleR.getString("username"));
            DetailGroup.add(DetailGroupMap);
            DetailGroupMap.put("TITLE", "Password: ");
            DetailGroupMap.put("CONTENT", bundleR.getString("password"));
            DetailGroup.add(DetailGroupMap);
            DetailList.add(DetailGroup);
            break;
        default:
            break;
    }
}

密码列表.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >
<TextView
    android:id="@+id/password_listing"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/app_name"
    android:textColor="@color/black"
    android:textSize="20dip"
    android:textStyle="bold" />
<Button
    android:id="@+id/b_add_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/add" />
<ExpandableListView 
    android:id="@+id/password_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

错误代码:

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

代码有什么问题?

4

1 回答 1

0

您的 xml 布局中按钮的 ID 与您传递给的 ID 不匹配findViewById();

<Button
    android:id="@+id/b_add_list"
    ....

b_addPwd = (Button)findViewById(R.id.b_addpwd);

“R.id.”之后的部分 应该匹配 "@+id/" 之后的部分 发生的事情是你得到 null 回来,findViewById();因为没有找到带有 id 的 View R.id.b_addpwd。然后,当您尝试调用.setOnClickListener()它时,可能会引发 NullPointer 异常。

于 2012-05-27T13:46:55.220 回答