2012-04-03 31 views
0

在我的XML中有一個按鈕,單擊它時會切換我的TableLayout的可見性。這是XML,在android中不工作的切換tableLayout的可見性的函數

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:id="@+id/LayoutAddOns" 
     android:orientation="vertical" > 

      <!-- Accordion button --> 
      <Button 
       android:id="@+id/DisplayAddOns" 
       android:layout_width="335dp" 
       android:layout_height="35dp" 
       android:drawableLeft="@drawable/rarrow" 
       android:onClick="ShowAddons" 
       android:text="AddOns (Optional)" /> 
      <!-- Table for add ons --> 
       <TableLayout 
       android:id="@+id/tableAddOns" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:visibility="gone" > 

         <TableRow 
          android:id="@+id/tableRow1" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          > 

          <CheckBox 
           android:id="@+id/CheckGalleryPlus" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="Gallery Plus" 
           android:textSize="14sp" /> 

          <CheckBox 
           android:id="@+id/CheckSubtitle" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="Subtitle" 
           android:textSize="14sp" /> 

         </TableRow> 
         <TableRow 
          android:id="@+id/tableRow2" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" > 

          <CheckBox 
           android:id="@+id/CheckDesigner" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="Designer" 
           android:textSize="14sp" /> 

          <CheckBox 
           android:id="@+id/CheckSupersize" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="Supersize" 
           android:textSize="14sp" /> 

         </TableRow> 

         <TableRow 
          android:id="@+id/tableRow3" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" > 

          <CheckBox 
           android:id="@+id/CheckScheduled" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="Scheduled" 
           android:textSize="14sp" /> 

          <CheckBox 
           android:id="@+id/CheckInternationalVisibility" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="International Visibility" 
           android:textSize="14sp" />         

         </TableRow> 

         <TableRow 
          android:id="@+id/tableRow4" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" > 


          <CheckBox 
           android:id="@+id/CheckBold" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="Bold" 
           android:textSize="14sp" /> 

          <CheckBox 
           android:id="@+id/CheckHighlight" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="Highlight" 
           android:textSize="14sp" />       

         </TableRow> 

         <TableRow 
          android:id="@+id/tableRow5" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" > 


          <CheckBox 
           android:id="@+id/CheckFeaturedList" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="Featured List" 
           android:textSize="14sp" /> 

          <CheckBox 
           android:id="@+id/CheckFeaturedPlus" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_marginLeft="10dp" 
           android:text="Featured Plus" 
           android:textSize="14sp" />       

         </TableRow> 

      </TableLayout> 

    </LinearLayout> 

這是logcat的數據,

04-03 01:21:52.891: D/AndroidRuntime(408): Shutting down VM 
04-03 01:21:52.891: W/dalvikvm(408): threadid=1: thread exiting with uncaught   exception (group=0x40014760) 
04-03 01:21:52.911: E/AndroidRuntime(408): FATAL EXCEPTION: main 
04-03 01:21:52.911: E/AndroidRuntime(408): java.lang.IllegalStateException: Could not find a method ShowAddons(View) in the activity class toggler.state.TogglerActivity for onClick handler on view class android.widget.Button with id 'DisplayAddOns' 
04-03 01:21:52.911: E/AndroidRuntime(408): at android.view.View$1.onClick(View.java:2670) 
04-03 01:21:52.911: E/AndroidRuntime(408): at android.view.View.performClick(View.java:3110) 
04-03 01:21:52.911: E/AndroidRuntime(408): at android.view.View$PerformClick.run(View.java:11934) 
04-03 01:21:52.911: E/AndroidRuntime(408): at android.os.Handler.handleCallback(Handler.java:587) 
04-03 01:21:52.911: E/AndroidRuntime(408): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-03 01:21:52.911: E/AndroidRuntime(408): at android.os.Looper.loop(Looper.java:132) 
04-03 01:21:52.911: E/AndroidRuntime(408): at android.app.ActivityThread.main(ActivityThread.java:4123) 
04-03 01:21:52.911: E/AndroidRuntime(408): at java.lang.reflect.Method.invokeNative(Native Method) 
04-03 01:21:52.911: E/AndroidRuntime(408): at java.lang.reflect.Method.invoke(Method.java:491) 
04-03 01:21:52.911: E/AndroidRuntime(408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
04-03 01:21:52.911: E/AndroidRuntime(408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
04-03 01:21:52.911: E/AndroidRuntime(408): at dalvik.system.NativeStart.main(Native Method) 
04-03 01:21:52.911: E/AndroidRuntime(408): Caused by: java.lang.NoSuchMethodException: ShowAddons [class android.view.View] 
04-03 01:21:52.911: E/AndroidRuntime(408): at java.lang.ClassMembers.getConstructorOrMethod(ClassMembers.java:235) 
04-03 01:21:52.911: E/AndroidRuntime(408): at java.lang.Class.getMethod(Class.java:904) 
04-03 01:21:52.911: E/AndroidRuntime(408): at android.view.View$1.onClick(View.java:2663) 
04-03 01:21:52.911: E/AndroidRuntime(408): ... 11 more 

,這是Java代碼,

public void ShowAddons(){ 

    int TableId = R.id.tableAddOns; 

    TableLayout TableVisibility = (TableLayout)findViewById(TableId); 


    if(TableVisibility.getVisibility() == View.VISIBLE) 
    { 
     TableVisibility.setVisibility(View.GONE); 
    } 

    else 
    { 
     TableVisibility.setVisibility(View.VISIBLE);  
    } 


} 

它在Eclipse中沒有錯誤,但是當我運行它,它說錯誤已經發生,強制關閉應用程序。謝謝。

+0

你可以發佈LogCat中的堆棧跟蹤,或者告訴我們你得到的錯誤是什麼?你能向我們展示你想要隱藏的表格的xml嗎? – 2012-04-03 01:15:14

+0

發佈了XML文件。 LogCat爲空,儘管 – rusty009 2012-04-03 01:19:19

+0

當您說系統告訴您「發生了錯誤」並強制關閉時,這表示發生了某種未捕獲的異常。您的應用程序可能沒有logcat輸出,但是如果關閉所有過濾,則應該有logcat輸出來顯示錯誤。 – 2012-04-03 01:22:06

回答

2

嘗試使ShowAddOns()採取View參數,因爲ShowAddOns(View target),即使您不使用方法中的目標視圖。

+0

現在完美運行,謝謝! – rusty009 2012-04-03 01:30:53

+0

不客氣!不要忘記註冊。 :) – 2012-04-03 01:39:45

+0

@JonO如果你不想在'onClick(View v)'上綁定它,怎麼做?我得到這個錯誤,如果我把它放在別的地方沒有參數:'MainActivity類型中的ShowAddOns(View)方法不適用於參數()' – user1480019 2013-08-12 09:28:54