gpt4 book ai didi

Android 应用程序在膨胀新 View 元素时崩溃

转载 作者:行者123 更新时间:2023-11-29 17:57:47 25 4
gpt4 key购买 nike


预期结果:

Android 版本 = 2.2

使用 inflate() 以编程方式将 RelativeLayout 添加到 main_menu.xml 中的 LinearLayout .线性布局(ID 是 main_menu.xml 中的 item_list)是一个用于保存项目列表的容器。每个项目都是一个相对布局,将在从服务器接收数据后以编程方式添加到回调函数中。

inflate()的用法从here中得知.


问题:

应用程序在执行此语句(错误消息中为 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

最佳答案

错误表明您有一个 ClassCastException,这意味着您没有正确转换类。对于布局参数与所用布局之间的关系,大多数人似乎都有疑问。

它应该是这样的:

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

LinearLayout 中的按钮

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

在您的情况下,LayoutParams 应该是 LinearLayout 类型。其他一切正常。

关于Android 应用程序在膨胀新 View 元素时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18095822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com