以下に示すようにボタンにビューを表示したいのですが、これができないので、検索して ViewGroup.addView() を使用する必要があることがわかりました。
これが私のMenu.javaアクティビティです
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
View v=null;
btn_timeTable=(Button)findViewById(R.id.btn_timeTable);
ViewGroup vg_timeTable = (ViewGroup)findViewById(R.id.btn_timeTable);
vg_timeTable.addView(getView(v, "TimeTable", R.drawable.table_1));
btn_subjects=(Button)findViewById(R.id.btn_subjects);
ViewGroup vg_subjects = (ViewGroup)findViewById(R.id.btn_subjects);
vg_subjects.addView(getView(v, "Subjects", R.drawable.books));
btn_lesson_timeTable=(Button)findViewById(R.id.btn_lesson_timeTable);
ViewGroup vg_lesson_timeTable = (ViewGroup)findViewById(R.id.btn_lesson_timeTable);
vg_lesson_timeTable.addView(getView(v, "Lesson\ntimeschedule", R.drawable.clock));
}
private View getView(View v, String text, int picture) {
Helper helper=null;
if (v == null) {
LayoutInflater inflater = getLayoutInflater();
v = inflater.inflate(R.layout.btn_view, null);
helper = new Helper(v);
v.setTag(helper);
} else {
helper = (Helper) v.getTag();
}
helper.setView(text, picture);
return v;
}
class Helper {
View v = null;
public Helper(View v) {
this.v = v;
}
void setView(String text, int picture) {
TextView tv_name = (TextView) v.findViewById(R.id.btn_text);
ImageView iv_picture = (ImageView) v.findViewById(R.id.btn_picture);
tv_name.setText(text);
iv_picture.setImageResource(picture);
}
}
ここに私の btn_view.xml ファイルがあります
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/tab_selector">
<ImageView
android:id="@+id/btn_picture"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_gravity="center_vertical|center_horizontal"
android:src="@drawable/kmenuedit" >
</ImageView>
<TextView
android:id="@+id/btn_text"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:focusable="true"
android:text="@string/text"
android:textColor="@color/White">
</TextView>
</LinearLayout>
しかし、ランタイム例外が発生します
ここに私のlogcatがあります
01-04 22:37:19.921: D/dalvikvm(392): GC_EXTERNAL_ALLOC freed 700 objects / 54672 bytes in 53ms
01-04 22:37:19.951: D/AndroidRuntime(392): Shutting down VM
01-04 22:37:19.951: W/dalvikvm(392): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-04 22:37:19.961: E/AndroidRuntime(392): FATAL EXCEPTION: main
01-04 22:37:19.961: E/AndroidRuntime(392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homeassignment/com.example.homeassignment.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homeassignment/com.example.homeassignment.Menus}: java.lang.ClassCastException: android.widget.Button
01-04 22:37:19.961: E/AndroidRuntime(392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-04 22:37:19.961: E/AndroidRuntime(392): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-04 22:37:19.961: E/AndroidRuntime(392): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-04 22:37:19.961: E/AndroidRuntime(392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-04 22:37:19.961: E/AndroidRuntime(392): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 22:37:19.961: E/AndroidRuntime(392): at android.os.Looper.loop(Looper.java:123)
01-04 22:37:19.961: E/AndroidRuntime(392): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-04 22:37:19.961: E/AndroidRuntime(392): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 22:37:19.961: E/AndroidRuntime(392): at java.lang.reflect.Method.invoke(Method.java:521)
01-04 22:37:19.961: E/AndroidRuntime(392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-04 22:37:19.961: E/AndroidRuntime(392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-04 22:37:19.961: E/AndroidRuntime(392): at dalvik.system.NativeStart.main(Native Method)
誰でも私を助けることができますか?前もって感謝します :)