English 中文(简体)
动态更新可扩展列表视图
原标题:Dynamic Update ExpandableListView

我正在用 xml 文件进行动态的扩展列表视图, 但是当我在列表中没有内容的布局中装入时, 我崩溃了 。 以下是我的代码 :

密码列表活动。

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)

代码有什么问题?

最佳回答

您 xml 版式中按钮的 ID 与您要传递到 finddViewById (); 的 ID 不匹配

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

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

“ R. id” 之后的部分应该匹配“ id/ ” 之后的部分, 正在发生的情况是, 您正在从 findedViewById (); 获得空号, 因为没有找到 id R. id.b_addpwd 的视图。 当你试图调用 . setOnClickListener () 时, 它可能会丢弃 NullPointer 例外 。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签