2012-05-27 5 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에서 참조 된 모든 strign이 존재하는지 확인하십시오. – FoamyGuy

+0

AndroidManifest.xml에 PasswordListingActivity를 추가하는 것을 잊어 버렸습니다. –

답변

0

는 "R.id."후 findViewById();

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

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

부분에 전달하는 ID와 일치하지 않는 XML 레이아웃에서 버튼의 ID "@ + id /"다음에 오는 부분과 일치해야합니다. 이라는 ID가있는 뷰가 없기 때문에 findViewById();에서 null이 반환되고 있습니다. 그런 다음 .setOnClickListener()에 전화하면 NullPointer 예외가 발생할 가능성이 높습니다.

+0

충돌이 발생했을 때 버튼을 클릭하지 않았습니다. passwordlisting.xml의 레이아웃을로드 할 때 충돌이 발생했습니다. –