-1

我想查看我的按钮,如下所示,但我不能这样做,所以我搜索并发现我应该使用 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>

但我得到运行时异常

这是我的日志猫

 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) 

谁能帮我?提前致谢 :)

4

3 回答 3

0

您在 R.layout.menu 中有问题。java.lang.ClassCastException: android.widget.Button 表示 android 无法将某些内容投射到按钮上。所以我认为 btn_timeTable (或 btn_subjects/btn_lesson_timeTable )不是一个按钮,你正试图将它转换为一个按钮,所以它失败并抛出异常。

于 2013-01-04T23:06:04.537 回答
0

我认为这可能是问题的一部分:

Unable to start activity ComponentInfo{com.example.homeassignment/com.example.homeassignment.MainActivity}

我猜想这条路应该是:

com.example.homeassignment/MainActivity
于 2013-01-04T23:01:59.283 回答
0

其他两个答案都对了一半。您似乎正在尝试将不是按钮的视图转换为按钮,但您的日志猫也表示它在您的主要活动中。在您的主要活动中查找您声明按钮的所有位置,并仔细检查 ID。

于 2013-01-04T23:11:46.307 回答