[英]Error while inflating custom layout on Android
幾天前,我開始制作一個Android應用程序,然后嘗試制作一個自定義小部件來顯示列表中每個項目的數據。 它擴展了相對布局。
我發現了這個: http : //www.anotherandroidblog.com/2011/05/26/custom-composite-android-component/
但是我的應用程序不再使用此代碼啟動。
相反,我有他們的錯誤:
ava.lang.RuntimeException:無法啟動活動ComponentInfo {com.example.kpsolver / com.example.kpsolver.MainActivity}:java.lang.ClassCastException:android.widget.RelativeLayout無法轉換為com.example.kpsolver.ItemEntry
在我的活動中:
private Problem pb;
private LinearLayout obj_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = new Problem(100);
obj_list = (LinearLayout) findViewById(R.id.objectsList);
ItemEntry itEntry = (ItemEntry) getLayoutInflater().inflate(R.layout.item_entry, obj_list, false);
obj_list.addView(itEntry);
}
ItemEntry.java
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
public class ItemEntry extends RelativeLayout {
private TextView name, value, number, weight;
boolean editable;
public ItemEntry(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.item_entry, this);
name = (TextView) findViewById(R.id.textItemName);
number = (TextView) findViewById(R.id.textItemUse);
value = (TextView) findViewById(R.id.textItemValue);
weight = (TextView) findViewById(R.id.textItemWeight);
}
public void setName(String str) {
name.setText(str);
}
public void setValue(Integer v) {
number.setText(v+" €");
}
}
item_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp" >
<TextView
android:id="@+id/textItemName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:gravity="left|center_vertical"
android:text="Nom" />
<TextView
android:id="@+id/textItemValue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/textItemWeight"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="Val" />
<TextView
android:id="@+id/textItemWeight"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/textItemUse"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="Nb" />
<TextView
android:id="@+id/textItemUse"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/buttonEditItem"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="Poids" />
<ImageButton
android:id="@+id/buttonEditItem"
android:layout_width="45dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_alignParentRight="true"
android:src="@drawable/ic_action_edit" />
</RelativeLayout>
嘗試這個:
obj_list = (LinearLayout) findViewById(R.id.objectsList);
ItemEntry itEntry = (RelativeLayout) getLayoutInflater().inflate(R.layout.item_entry, null, false);
obj_list.addView(itEntry);
盡管不能完全確定您的錯誤,因為您沒有發布完整的XML
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.