1

我在 OnCreate 方法上调用以下方法 setupAdapter();

  private void setupAdapter() {

    // set the list adapter
    JSONArray returnArray = null;
    try {
        returnArray = getQuickViewContents();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject obj0 = null;
    if (returnArray != null) {
        try {
            obj0 = returnArray.getJSONObject(0);

        } catch (JSONException e) {
            e.printStackTrace();

        }
    }
    try {
        getCreditCardDetails();
    } catch (JSONException e3) {
        e3.printStackTrace();
    }

    JSONArray productArray = null;
    try {
        productArray = obj0.getJSONArray("Content");
    } catch (JSONException e2) {
        e2.printStackTrace();
    }

    ArrayList<HashMap<String, String>> productList = new ArrayList<HashMap<String, String>>();



    if (productArray != null) {

        ListView lv = (ListView) findViewById(android.R.id.list);

        for (int i = 0; i < productArray.length(); i++) {

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap
            try {
                map.put(KEY_BRAND,
                        productArray.getJSONObject(i).getString("Brand"));
                map.put(KEY_QUANTITY_NAME, productArray.getJSONObject(i)
                        .getString("Quantity")
                        + " X "
                        + productArray.getJSONObject(i).getString("Name"));
                map.put(KEY_PRICE, Currency.britishPound
                        + productArray.getJSONObject(i)
                                .getString("MinCost"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            // adding HashList to ArrayList
            productList.add(map);


            adapter = new APIQuickCheckoutProductProvider(APIQuickCheckout.this, productList);
            lv.addHeaderView(headerMethod());


            lv.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        }
    }
}

private View headerMethod() {

    View header = getLayoutInflater().inflate(R.layout.api_quick_checkout_header, null);
    card_cvv = (EditText) header.findViewById(R.id.api_quick_checkout_card_cvv);

    Button edit_payment_details = (Button) header.findViewById(R.id.edit_button_payment);
    edit_payment_details.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            final Intent i = new Intent(APIQuickCheckout.this,APIPaymentDetails.class);
            startActivity(i);
        }
    });

    // set the payment info layout
    TextView card_number = (TextView) header.findViewById(R.id.api_quick_checkout_card_number);
    TextView card_expiry = (TextView) header.findViewById(R.id.api_quick_checkout_card_expiry);
    try {
        card_number.setText(CheckoutManager.getInstance().getSelectedPaymentCard().getString("cardNumber"));
        card_expiry.setText(CheckoutManager.getInstance().getSelectedPaymentCard().getString("expDate"));
    } catch (Exception e1) {
        e1.printStackTrace();
    }

    return header;

}

和堆栈跟踪

 12-24 17:31:17.805: E/AndroidRuntime(31051): FATAL EXCEPTION: main
 12-24 17:31:17.805: E/AndroidRuntime(31051): java.lang.RuntimeException: Unable to start     activity ComponentInfo{apicheckout.APIQuickCheckout}: java.lang.IllegalStateException:   Cannot add header view to list -- setAdapter has already been called.
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.os.Handler.dispatchMessage(Handler.java:99)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.os.Looper.loop(Looper.java:130)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.app.ActivityThread.main(ActivityThread.java:3683)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at java.lang.reflect.Method.invokeNative(Native Method)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at java.lang.reflect.Method.invoke(Method.java:507)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at dalvik.system.NativeStart.main(Native Method)
 12-24 17:31:17.805: E/AndroidRuntime(31051): Caused by: java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.widget.ListView.addHeaderView(ListView.java:279)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.widget.ListView.addHeaderView(ListView.java:302)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at apicheckout.APIQuickCheckout.setupAdapter(APIQuickCheckout.java:192)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at apicheckout.APIQuickCheckout.onCreate(APIQuickCheckout.java:121)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 12-24 17:31:17.805: E/AndroidRuntime(31051):   ... 11 more

我要做的就是将 headerMethod 的内容添加为标题。我究竟做错了什么?

4

2 回答 2

3

将这些代码行置于for 循环之外

adapter = new APIQuickCheckoutProductProvider(APIQuickCheckout.this, productList);
lv.addHeaderView(headerMethod());
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
于 2012-12-24T17:52:09.487 回答
2

抛出错误是因为您在调用后尝试添加标头,setAdapter(...)因为它们所在的 for 循环不止一次运行。

于 2012-12-24T18:29:57.830 回答