我正在使用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;
}
}
passwordlisting.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)
代码有什么问题?
答案 0 :(得分:0)
xml布局中按钮的ID与您传递给findViewById();
的ID不匹配
<Button
android:id="@+id/b_add_list"
....
和
b_addPwd = (Button)findViewById(R.id.b_addpwd);
“R.id”之后的部分应该匹配“@ + id /”之后的部分正在发生的事情是您从findViewById();
返回null,因为没有找到ID为R.id.b_addpwd
的View。然后,当您尝试调用.setOnClickListener()
时,可能会抛出NullPointer异常。