1

预期结果:

安卓版本 = 2.2

以编程方式在using中添加一个RelativeLayoutto 。线性布局(ID在 中)是一个用于保存项目列表的容器。每个项目都是一个相对布局,并且将在从服务器接收数据后以编程方式添加到回调函数中。LinearLayoutmain_menu.xmlinflate()item_listmain_menu.xml

的用法inflate()就是从这里学习的。


问题:

应用程序在运行此语句时崩溃(错误消息中为 MainActivity.java:693),

RelativeLayout relativeLayout = (RelativeLayout) RelativeLayout.inflate(MainActivity.this, R.layout.template_item_list_cell, linearLayoutItemList);

但是,如果将上述语句替换为以下语句,则该应用程序运行良好updateItemList()

RelativeLayout relativeLayout = new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, height);
relativeLayout.setLayoutParams(relativeLayoutParams);
relativeLayout.setBackgroundResource(R.color.cell_state);
relativeLayout.setClickable(true);

并插入此语句,

linearLayoutItemList.addView(relativeLayout);

评论后,

// Add the RelativeLayout to LinearLayout

错误信息:

08-07 13:54:44.689: W/dalvikvm(23363): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
08-07 13:54:44.699: E/AndroidRuntime(23363): FATAL EXCEPTION: main
08-07 13:54:44.699: E/AndroidRuntime(23363): java.lang.ClassCastException: android.widget.LinearLayout
08-07 13:54:44.699: E/AndroidRuntime(23363):    at com.domainname.appname.android.MainAc$12$1.run(MainActivity.java:693)
08-07 13:54:44.699: E/AndroidRuntime(23363):    at android.os.Handler.handleCallback(Handler.java:587)
08-07 13:54:44.699: E/AndroidRuntime(23363):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 13:54:44.699: E/AndroidRuntime(23363):    at android.os.Looper.loop(Looper.java:130)
08-07 13:54:44.699: E/AndroidRuntime(23363):    at android.app.ActivityThread.main(ActivityThread.java:3691)
08-07 13:54:44.699: E/AndroidRuntime(23363):    at java.lang.reflect.Method.invokeNative(Native Method)
08-07 13:54:44.699: E/AndroidRuntime(23363):    at java.lang.reflect.Method.invoke(Method.java:507)
08-07 13:54:44.699: E/AndroidRuntime(23363):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
08-07 13:54:44.699: E/AndroidRuntime(23363):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
08-07 13:54:44.699: E/AndroidRuntime(23363):    at dalvik.system.NativeStart.main(Native Method)

代码

res/layout/main_menu.xml

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#555555"
    android:orientation="vertical" >

</LinearLayout>

res/layout/template_item_list_cell.xml

<?xml version="1.0" encoding="utf-8"? >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="41dp"
    android:background="@color/cell_state"
    android:clickable="true"
    android:visibility="gone" >

</RelativeLayout>

res/color/cell_state.xml

<?xml version="1.0" encoding="utf-8"? >
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Active state -->
    <item
        android:state_selected="true"
        android:state_focused="false"
        android:state_pressed="false"
        android:drawable="@color/MyDarkGray" />

    <!-- Inactive state-->
    <item 
        android:state_selected="false" 
        android:state_focused="false"
        android:state_pressed="false" 
        android:drawable="@color/MyGray" />

    <!-- Pressed state-->
    <item 
        android:state_pressed="true"
        android:drawable="@color/MyDarkGray" />

    <!-- Selected state (using d-pad) -->
    <item
        android:state_focused="true" 
        android:state_selected="true"
        android:state_pressed="false" 
        android:drawable="@color/MyDarkGray" />

</selector>

res/values/customized_colors.xml

<?xml version="1.0" encoding="utf-8"? >
<resources>
    <color name="MyGray">#555555</color>
    <color name="MyDarkGray">#333333</color>
</resources>

src/com.domainname.appname.android/MainActivity.java

public class MainActivity extends Activity {
    @Override
    public void onCreate{
        // Some initialization
        setContentView(R.layout.main_menu);
        // Some other initialization
    }
    public void updateItemList() {
        // Remove previous items in the list
        LinearLayout linearLayoutItemList = (LinearLayout) menu.findViewById(R.id.item_list); // The linear layout is given an ID of item_list in main_menu.xml
        linearLayoutItemList.removeAllViews();

        // Request to server and define the callback function to add item(s) to the list programmatically

        JSONObject json = new JSONObject();
    MyRequestClass request = new MyRequestClass();

    request.request(MyConstants.WEB_BASE_URL, "app/get_latest_item_list", json, new MyCallbackClass() {
            @Override
            public void run(final JSONObject json) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String jsonInString = json.toString();
                            JSONArray jsonArray = json.getJSONArray("data");

                            // Suppose the JSON is properly returned
                            int numberOfItemListItems = jsonArray.length();

                            RelativeLayout relativeLayoutItemListTitleBar = (RelativeLayout) menu.findViewById(R.id.item_list_title_bar);
                            View viewDivider1 = (View) menu.findViewById(R.id.item_list_divider_1);
                            View viewDivider2 = (View) menu.findViewById(R.id.item_list_divider_2);
                            LinearLayout linearLayoutItemList = (LinearLayout) menu.findViewById(R.id.item_list);
                            View viewDivider3 = (View) menu.findViewById(R.id.item_list_divider_3);
                            View viewDivider4 = (View) menu.findViewById(R.id.item_list_divider_4);

                            if (0 == numberOfItemListItems) {
                                relativeLayoutItemListTitleBar.setVisibility(RelativeLayout.GONE);
                                viewDivider1.setVisibility(View.GONE);
                                viewDivider2.setVisibility(View.GONE);
                                linearLayoutItemList.setVisibility(LinearLayout.GONE);
                                viewDivider3.setVisibility(View.GONE);
                                        viewDivider4.setVisibility(View.GONE);
                            } else {
                                relativeLayoutItemListTitleBar.setVisibility(RelativeLayout.VISIBLE);
                                viewDivider1.setVisibility(View.VISIBLE);
                                viewDivider2.setVisibility(View.VISIBLE);
                                linearLayoutItemList.setVisibility(LinearLayout.VISIBLE);
                                viewDivider3.setVisibility(View.VISIBLE);
                                    viewDivider4.setVisibility(View.VISIBLE);

                                for (int index = 0; index < numberOfItemListItems; index++) {
                                    try {
                                        JSONObject currentJsonObject = jsonArray.getJSONObject(index);

                                        // Extract item title
                                        String currentTitle = currentJsonObject.getString("Title");

                                        // Extract icon image URI
                                        final String currentIconImageUri = ITEM_ICON_IMAGE_BASE_URL + currentJsonObject.getString("IconImageUri");

                                        // Extract item ID
                                        final String currentItemId = currentJsonObject.getString("ItemIdUri");

                                        // Create a cell to display the item
                                        linearLayoutItemList = (LinearLayout) menu.findViewById(R.id.item_list);

                                        // Create RelativeLayout for the cell
                                        int height = (int) (getResources().getDisplayMetrics().density * 41 + 0.5f); // 41dp height

                                        RelativeLayout relativeLayout = (RelativeLayout) LinearLayout.inflate(linearLayoutItemList.getContext(), R.layout.template_item_list_cell, linearLayoutItemList);
                                        relativeLayout.setOnClickListener(new View.OnClickListener(){
                                             @Override
                                             public void onClick(View v){
                                                 selectItemListCell(currentItemId);
                                             }
                                         });

                                        // Add icon image to a ImageView and add that TextView to RelativeLayout
                                        ImageView imageView = new ImageView(MainActivity.this);
                                        imageView = loadImageWithUrlString(imageView, currentIconImageUri);
                                        int width = (int) (getResources().getDisplayMetrics().density * 31 + 0.5f); // 31dp height
                                        height = (int) (getResources().getDisplayMetrics().density * 31 + 0.5f); // 31dp height
                                        RelativeLayout.LayoutParams relativeLayoutParams1 = new RelativeLayout.LayoutParams(width, height);
                                        relativeLayoutParams1.addRule(RelativeLayout.CENTER_VERTICAL);
                                        relativeLayoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                                        int marginLeft = (int) (getResources().getDisplayMetrics().density * 10 + 0.5f); // 10dp
                                        int marginTop = (int) (getResources().getDisplayMetrics().density * 5 + 0.5f); // 5dp
                                        int marginRight = (int) (getResources().getDisplayMetrics().density * 0 + 0.5f); // 0dp
                                        int marginBottom = (int) (getResources().getDisplayMetrics().density * 5 + 0.5f); // 5dp
                                        relativeLayoutParams1.setMargins(marginLeft, marginTop, 0, marginBottom);
                                        imageView.setLayoutParams(relativeLayoutParams1);
                                        relativeLayout.addView(imageView);

                                        // Add title text to a TextView and add that TextView to RelativeLayout
                                        TextView textView = new TextView(MainActivity.this);
                                        textView.setText(currentTitle);
                                        float shadowRadius = 2.0f;
                                        int shadowDx = 0;
                                        int shadowDy = 1;
                                        int shadowColor = 0xFF000000;
                                        textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor);
                                        RelativeLayout.LayoutParams relativeLayoutParams2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                        relativeLayoutParams2.addRule(RelativeLayout.CENTER_VERTICAL);
                                        marginLeft = (int) (getResources().getDisplayMetrics().density * 56 + 0.5f); // 56dp
                                        marginRight = (int) (getResources().getDisplayMetrics().density * 43 + 0.5f); // 43dp
                                        relativeLayoutParams2.setMargins(marginLeft, 0, marginRight, 0);
                                        textView.setLayoutParams(relativeLayoutParams2);
                                        relativeLayout.addView(textView);

                                        // Add the RelativeLayout to LinearLayout
                                        //   linearLayoutItemList.addView(relativeLayout);

                                        // Add divider to the LinearLayout
                                        if (index < (numberOfItemListItems - 1)) {

                                            View view1 = new View(MainActivity.this);
                                            height = (int) (getResources().getDisplayMetrics().density * 0.5 + 0.5f); // 0.5dp height
                                            RelativeLayout.LayoutParams relativeLayoutParams3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, height);
                                            view1.setLayoutParams(relativeLayoutParams3);
                                            view1.setBackgroundColor(0xFF323232);
                                            linearLayoutItemList.addView(view1);

                                            View view2 = new View(MainActivity.this);
                                            height = (int) (getResources().getDisplayMetrics().density * 0.5 + 0.5f); // 0.5dp height
                                            RelativeLayout.LayoutParams relativeLayoutParams4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, height);
                                            view2.setLayoutParams(relativeLayoutParams4);
                                            view2.setBackgroundColor(0xFF595959);
                                            linearLayoutItemList.addView(view2);
                                        }

                                    } catch (JSONException e) {
                                        // Oops
                                    } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                    }
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }); // End of runOnUiThread()
            }
        });


    } // End of updateItemList
} // End of MainActivity
4

2 回答 2

2

错误说你有一个ClassCastException,这意味着你没有正确地转换类。当谈到布局参数和使用的布局之间的关系时,大多数人似乎都有问题。

这是它应该是这样的:

您应该为您的布局分配父类型的布局参数。例如,假设你有这个

Button inside a LinearLayout

如果你想设置LayoutParams它们button必须LinearLayout.LayoutParams是按钮父类型ViewGroup(每个布局本质上是一个 ViewGroup - LinearLayout、RelativeLayout 等。)

在您的情况下,LayoutParams 应该是 LinearLayout 类型。其他一切都很好。

于 2013-08-07T06:19:42.677 回答
1

解决方法

改变那个错误的说法,

RelativeLayout relativeLayout = (RelativeLayout) RelativeLayout.inflate(MainActivity.this, R.layout.template_item_list_cell, linearLayoutItemList);

进入

RelativeLayout relativeLayout = (RelativeLayout) RelativeLayout.inflate(MainActivity.this, R.layout.template_item_list_cell, null);
linearLayoutItemList.addView(relativeLayout);

或者

RelativeLayout relativeLayout = (RelativeLayout) MainActivity.this.getLayoutInflater().inflate(R.layout.template_item_list_cell, null);
linearLayoutItemList.addView(relativeLayout);

更新

null但是,由于参数中的该值,上述两个未能保留布局参数信息ViewGroup root

因此,根据Luksprog这篇文章中的回答,更好的解决方案是指出正确的布局参数信息,

RelativeLayout relativeLayout = (RelativeLayout) MainActivity.this.getLayoutInflater().inflate(R.layout.template_item_list_cell, (LinearLayout) linearLayoutClubList, false);
linearLayoutItemList.addView(relativeLayout);
于 2013-08-07T07:37:36.787 回答