我几天前开始做一个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>
答案 0 :(得分:0)
试试这个:
obj_list = (LinearLayout) findViewById(R.id.objectsList);
ItemEntry itEntry = (RelativeLayout) getLayoutInflater().inflate(R.layout.item_entry, null, false);
obj_list.addView(itEntry);
虽然不完全确定您的错误,但因为您没有发布完整的XML