2013-01-04 25 views
-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> 

,但我得到的运行时异常

,这里是我的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) 

谁能帮我?感谢提前:)

回答

0

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

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

在猜测我想的路径,就是要:

com.example.homeassignment/MainActivity 
+0

我是否正确使用ViewGroupe.addView()? –

0

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

+0

当我删除这些ViewGroup vg_timeTable =(ViewGroup)findViewById(R.id.btn_timeTable); vg_timeTable.addView(getView(v,“TimeTable”,R.drawable.table_1));它的工作原理,因此它表明问题出在那里,但我不能修复它 –

+0

更改“按钮”为btn_timeTable的视图真实的类名(LinearLayout/RelativeLayout/FrameLayout我无法准确告诉你,因为我看不到你的R.layout .menu xml内容) – Leonidos

+0

或更好显示我们R.layout.menu conent)你的代码很奇怪) – Leonidos

0

另外两个答案都是右半边。看起来你试图将一个不是按钮的视图作为一个按钮,但是你的日志猫也会说它在你的主要活动中。查看您的主要活动,查找您声明按钮的所有地方,然后仔细检查ID。

相关问题