问题描述:

I have a button in my XML, that when clicked will toggle the visibility of my TableLayout. This is the XML,

 <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>

This is the data from 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

and this is the Java code,

 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);

}

}

It has no errors in eclipse, but when I run it, it says am error has occured and force closes the application. Thanks.

网友答案:

Try making ShowAddOns() take a View argument, as ShowAddOns(View target), even if you don't use the target view in the method.

相关阅读:
Top