0

我确实把android:clickable="true",android:onClick="iconViewLink4"LinearLayout

所以,iconViewLink4当它被点击时应该被调用。
但是,当我单击它时,此应用程序将关闭。

这段代码有什么问题?

布局(activity_behind_left_simple.xml)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/leftBehindMenuScroll" >

    <LinearLayout style="@style/behindMenuScrollContent"
        android:paddingTop="25dp" >

        <TextView
            style="@style/behindMenuItemTitle"
            android:text="People" />

        <View 
           android:layout_width="fill_parent"
           android:layout_height="1dp"
           android:layout_marginBottom= "5dp"
           android:background="#d2d2d2"/>

        <LinearLayout
            android:id="@+id/iconViewLink4"
            android:layout_width="match_parent"
            android:clickable="true"
            android:onClick="iconViewLink4"
            android:layout_height="wrap_content" >

            <jp.fureco.IconView 
                android:id="@+id/iconViewItem4"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:textSize="20dp"/>

            <TextView
                android:layout_marginLeft="10dp"
                style="@style/behindMenuItemLabel"
                android:text="visitor" />

        </LinearLayout>

    </LinearLayout>

</ScrollView>

MainActivity.java

public class MainActivity extends Activity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNav = new SimpleSideDrawer(this);
        mNav.setLeftBehindContentView(R.layout.activity_behind_left_simple);
        mNav.setRightBehindContentView(R.layout.activity_behind_right_simple);
        ...
    }

    public void iconViewLink4(View view) {
        Toast.makeText(this, "This is test!", Toast.LENGTH_SHORT).show();
    }

}

日志猫

08-13 06:12:53.278: E/Trace(26398): error opening trace file: No such file or directory (2)
08-13 06:12:53.648: E/chromium(26398): external/chromium/net/disk_cache/stat_hub.cc:216: [0813/061253:ERROR:stat_hub.cc(216)] StatHub::Init - App com.sample isn't supported.
08-13 06:12:53.658: E/chromium(26398): external/chromium/net/disk_cache/stat_hub.cc:216: [0813/061253:ERROR:stat_hub.cc(216)] StatHub::Init - App com.sample isn't supported.
08-13 06:12:59.664: E/GLUtils(26398): GL ERROR - after deleteUnusedTextures() glError (0x502)
08-13 06:13:00.585: E/AndroidRuntime(26398): FATAL EXCEPTION: main
08-13 06:13:00.585: E/AndroidRuntime(26398): java.lang.IllegalStateException: Could not find a method iconViewLink4(View) in the activity class android.app.Application for onClick handler on view class android.widget.LinearLayout with id 'iconViewLink4'
08-13 06:13:00.585: E/AndroidRuntime(26398):    at android.view.View$1.onClick(View.java:3596)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at android.view.View.performClick(View.java:4102)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at android.view.View$PerformClick.run(View.java:17085)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at android.os.Handler.handleCallback(Handler.java:615)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at android.os.Looper.loop(Looper.java:155)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at android.app.ActivityThread.main(ActivityThread.java:5511)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at java.lang.reflect.Method.invokeNative(Native Method)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at java.lang.reflect.Method.invoke(Method.java:511)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at dalvik.system.NativeStart.main(Native Method)
08-13 06:13:00.585: E/AndroidRuntime(26398): Caused by: java.lang.NoSuchMethodException: iconViewLink4 [class android.view.View]
08-13 06:13:00.585: E/AndroidRuntime(26398):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at java.lang.Class.getMethod(Class.java:915)
08-13 06:13:00.585: E/AndroidRuntime(26398):    at android.view.View$1.onClick(View.java:3589)
08-13 06:13:00.585: E/AndroidRuntime(26398):    ... 11 more
4

1 回答 1

1

您无法使用 android:onClick 为 Button 注册 onClick 回调,因为 Android 将无法找到它,因为它会在 Activity 类中搜索与 id 名称匹配的方法,即 iconViewLink4 并且它会抛出您遇到的异常. 为了克服对 iconViewLink4 的搜索并为其分配一个普通的侦听器。

于 2013-08-12T21:35:22.363 回答