当我使用选择器作为我的按钮时,我的应用程序崩溃了...... 我会疯了..我在互联网上研究但我找不到真正的解决方案
我的按钮代码:
<Button
android:id="@+id/ib_login"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_alignLeft="@+id/et_password"
android:layout_below="@+id/et_password"
android:layout_marginTop="20dp"
android:background="@drawable/login_selector" />
我的选择码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/button_onclick"
/>
<item android:drawable="@drawable/button"/>
</selector>
我的logcat:
04-18 06:52:37.084: E/AndroidRuntime(608): FATAL EXCEPTION: main
04-18 06:52:37.084: E/AndroidRuntime(608): java.lang.IllegalStateException: Could not find a method res/drawable-hdpi/button_onclick.png(View) in the activity class com.kisirdongu.denemeapp.MainActivity for onClick handler on view class android.widget.RelativeLayout with id 'container'
04-18 06:52:37.084: E/AndroidRuntime(608): at android.view.View$1.onClick(View.java:3031)
04-18 06:52:37.084: E/AndroidRuntime(608): at android.view.View.performClick(View.java:3511)
04-18 06:52:37.084: E/AndroidRuntime(608): at android.view.View$PerformClick.run(View.java:14105)
04-18 06:52:37.084: E/AndroidRuntime(608): at android.os.Handler.handleCallback(Handler.java:605)
04-18 06:52:37.084: E/AndroidRuntime(608): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 06:52:37.084: E/AndroidRuntime(608): at android.os.Looper.loop(Looper.java:137)
04-18 06:52:37.084: E/AndroidRuntime(608): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-18 06:52:37.084: E/AndroidRuntime(608): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 06:52:37.084: E/AndroidRuntime(608): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 06:52:37.084: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-18 06:52:37.084: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-18 06:52:37.084: E/AndroidRuntime(608): at dalvik.system.NativeStart.main(Native Method)
04-18 06:52:37.084: E/AndroidRuntime(608): Caused by: java.lang.NoSuchMethodException: res/drawable-hdpi/button_onclick.png [class android.view.View]
04-18 06:52:37.084: E/AndroidRuntime(608): at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-18 06:52:37.084: E/AndroidRuntime(608): at java.lang.Class.getMethod(Class.java:915)
04-18 06:52:37.084: E/AndroidRuntime(608): at android.view.View$1.onClick(View.java:3024)
04-18 06:52:37.084: E/AndroidRuntime(608): ... 11 more
*整体布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C7C7C7"
android:onClick="@drawable/button_onclick"
android:orientation="vertical"
tools:context="com.kisirdongu.denemeapp.MainActivity"
tools:ignore="MergeRootFrame" >
<EditText
android:id="@+id/et_password"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_below="@+id/et_username"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/edittext"
android:drawablePadding="10dp"
android:ems="10"
android:hint="*****"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:password="true"
android:singleLine="true" />
<Button
android:id="@+id/ib_login"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_alignLeft="@+id/et_password"
android:layout_below="@+id/et_password"
android:layout_marginTop="20dp"
android:background="@drawable/login_selector" />
<ProgressBar
android:id="@+id/pb_login"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ib_login"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:visibility="invisible" />
<TextView
android:id="@+id/tv_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pb_login"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:text="Logging in..."
android:visibility="invisible" />
<EditText
android:id="@+id/et_username"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_alignLeft="@+id/et_password"
android:layout_alignParentTop="true"
android:layout_marginTop="52dp"
android:background="@drawable/edittext"
android:drawablePadding="10dp"
android:ems="10"
android:hint="username"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:singleLine="true" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tv_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="User Login"
android:textColor="#333"
android:textSize="20sp" />
</RelativeLayout>
*我的drawable forlder:
感谢您的帮助!
答案 0 :(得分:0)
在我看来,你需要在创建自定义背景时定义所有三种状态,即默认,按下和聚焦按钮。还要确保您的图像具有Nine-patch位图 你只定义了两个,这是一个例子
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused"
android:state_focused="true" />
<item android:drawable="@drawable/button_default" />
</selector>
如需更多帮助,请访问此处 http://developer.android.com/guide/topics/ui/controls/button.html
答案 1 :(得分:0)
android:onClick="@drawable/button_onclick"
中的RelativeLayout
行会导致问题。它需要一个方法的名称,但是你传递了一个drawable。
答案 2 :(得分:0)
从布局中删除android:onClick="@drawable/button_onclick"
行
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C7C7C7"
android:orientation="vertical"
tools:context="com.kisirdongu.denemeapp.MainActivity"
tools:ignore="MergeRootFrame" >