gpt4 book ai didi

android - 为自定义操作栏项目设置 onClick 事件

转载 作者:行者123 更新时间:2023-12-05 07:55:16 25 4
gpt4 key购买 nike

当我尝试在自定义 ActionBar 的项目上添加 onClick 事件时,出现以下异常。当我在 ActionBar 中单击 searchiconactionbar 时,我的应用程序崩溃了。我已将方法 onClick() 放入 Activity 中只有。为什么我会收到此错误?是否有任何其他方法可以为自定义 ActionBar 项目设置 onClickListener?

java.lang.IllegalStateException: Could not find a method clickEvent(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageView with id 'searchiconactionbar'
at android.view.View$1.onClick(View.java:3838)
at android.view.View.performClick(View.java:4466)
at android.view.View$PerformClick.run(View.java:18542)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: clickEvent [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getMethod(Class.java:857)
at android.view.View$1.onClick(View.java:3831)
            at android.view.View.performClick(View.java

actionbar_gip

<RelativeLayout
android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/tabbackground"

xmlns:android="http://schemas.android.com/apk/res/android">

<EditText
android:id="@+id/actionsearchtext"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000000"
android:visibility="gone"
android:maxLines="1"
android:singleLine="true"

android:padding="7dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:hint="Search for all GIFs"
android:textColorHint="#544E4B"
android:layout_toLeftOf="@+id/usericonlayout"
android:background="@color/border_gray"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/usericonlayout"

>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/searchiconactionbar"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_centerInParent="true"
android:onClick="clickEvent"
android:layout_marginLeft="5dp"
android:src="@drawable/search"
/>
</RelativeLayout>

</RelativeLayout>

代码-

        LayoutInflater mInflater2 = LayoutInflater.from(this);
View mCustomView2 = mInflater.inflate(R.layout.actionbar_gip, null);
searchtext=(EditText)mCustomView2.findViewById(R.id.actionsearchtext);
searchIcon=(ImageView)mCustomView2.findViewById(R.id.searchiconactionbar);
getSupportActionBar().setCustomView(R.layout.actionbar_gip);
public void clickEvent(View v) {
if (v.getId() == R.id.searchiconactionbar) {
if (searchtext.getVisibility() == View.GONE)
expand(searchtext);
else
collapse(searchtext);
}

}

最佳答案

替换

getSupportActionBar().setCustomView(R.layout.actionbar_gip);

通过

 getSupportActionBar().setCustomView(mCustomView2);

解决了我的问题..

关于android - 为自定义操作栏项目设置 onClick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30036105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com