2012-05-27 45 views
0

我正在用xml文件做動態ExpandableListView,但是當我加載到列表中沒有內容的佈局時,我崩潰了。以下是我的代碼:動態更新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

發佈放樣軌跡。 – FoamyGuy

+0

查找堆棧跟蹤中指向您的.java文件之一的部分。張貼一個和所有的東西立即圍繞它。如果沒有任何行包含您的.java文件之一,請嘗試清理您的項目。並確保您的佈局xml中引用的所有條紋都存在。 – FoamyGuy

+0

忘記將PasswordListingActivity添加到AndroidManifest.xml中 –

回答

0

按鈕在你的XML佈局不相匹配的ID要傳遞到findViewById();

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

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

部分 「R.id.」 後的ID應匹配「@ + id /」之後的部分發生的事情是,您從findViewById();得到空值,因爲沒有找到與ID爲R.id.b_addpwd的View。然後,當您嘗試撥打.setOnClickListener()時,可能會拋出NullPointer異常。

+0

崩潰時,我沒有點擊按鈕。它在加載passwordlisting.xml的佈局時崩潰。 –