为自定义操作栏项目设置 onClick 事件
Setting onClick event for Custom Action Bar items
当我尝试在自定义 ActionBar.My 项目上添加 onClick 事件时出现以下异常,当我单击 ActionBar.I 中的 searchiconactionbar
时应用程序崩溃并已将方法 onClick( ) 在 activity only.Why 中,我收到这个错误了吗?还有其他方法可以为自定义 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.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.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);
解决了我的问题..
这是一个示例,它具有操作栏中自定义视图的 onClick() 功能。
自定义操作栏的布局:
<ImageView
android:id="@+id/custom_actionbar_back_iv"
android:layout_width="@dimen/small_margin_ar"
android:layout_height="@dimen/very_small_margin_ar"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/up_navigation"/>
<TextView
android:id="@+id/custom_actionbar_titleText_tv"
style="@style/wrapscreen"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/custom_actionbar_back_iv"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white"
android:textSize="@dimen/above_medium_text_size" />
<ImageButton
android:id="@+id/custom_actionbar_goToArchiveActivity_ib"
style="@style/wrapscreen"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/medium_margin"
android:background="@null"
android:src="@drawable/ic_action_overflow" />
下面是实现..
在 onCreate() 方法中定义 setupactionBar() 方法
private void setUpActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View customActionBarView = layoutInflater.inflate(R.layout.custom_actionbar, null);
// this view is used to show tile
TextView ActivityTitleTV = (TextView) customActionBarView.findViewById(R.id.custom_actionbar_titleText_tv);
ActivityTitleTV.setText(R.string.title_text_archive);
//this view can be used for back navigation
ImageView backToRecorderIV = (ImageView) customActionBarView.findViewById(R.id.custom_actionbar_back_iv);
backToRecorderIV.setVisibility(View.VISIBLE);
backToRecorderIV.setOnClickListener(this);
//Another view which has up navigation
ImageButton goToArchiveActivityIB = (ImageButton) customActionBarView.findViewById(R.id.custom_actionbar_goToArchiveActivity_ib);
goToArchiveActivityIB.setVisibility(View.GONE);
actionBar.setCustomView(customActionBarView);
actionBar.setDisplayShowCustomEnabled(true);
}
//listener for back navigation
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.custom_actionbar_back_iv:
Intent recorder = new Intent(ArchiveActivity.this, RecorderActivity.class);
startActivity(recorder);
finish();
break;
}
}
希望这对您有所帮助...
当我尝试在自定义 ActionBar.My 项目上添加 onClick 事件时出现以下异常,当我单击 ActionBar.I 中的 searchiconactionbar
时应用程序崩溃并已将方法 onClick( ) 在 activity only.Why 中,我收到这个错误了吗?还有其他方法可以为自定义 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.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.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);
解决了我的问题..
这是一个示例,它具有操作栏中自定义视图的 onClick() 功能。
自定义操作栏的布局:
<ImageView
android:id="@+id/custom_actionbar_back_iv"
android:layout_width="@dimen/small_margin_ar"
android:layout_height="@dimen/very_small_margin_ar"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/up_navigation"/>
<TextView
android:id="@+id/custom_actionbar_titleText_tv"
style="@style/wrapscreen"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/custom_actionbar_back_iv"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white"
android:textSize="@dimen/above_medium_text_size" />
<ImageButton
android:id="@+id/custom_actionbar_goToArchiveActivity_ib"
style="@style/wrapscreen"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/medium_margin"
android:background="@null"
android:src="@drawable/ic_action_overflow" />
下面是实现..
在 onCreate() 方法中定义 setupactionBar() 方法
private void setUpActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View customActionBarView = layoutInflater.inflate(R.layout.custom_actionbar, null);
// this view is used to show tile
TextView ActivityTitleTV = (TextView) customActionBarView.findViewById(R.id.custom_actionbar_titleText_tv);
ActivityTitleTV.setText(R.string.title_text_archive);
//this view can be used for back navigation
ImageView backToRecorderIV = (ImageView) customActionBarView.findViewById(R.id.custom_actionbar_back_iv);
backToRecorderIV.setVisibility(View.VISIBLE);
backToRecorderIV.setOnClickListener(this);
//Another view which has up navigation
ImageButton goToArchiveActivityIB = (ImageButton) customActionBarView.findViewById(R.id.custom_actionbar_goToArchiveActivity_ib);
goToArchiveActivityIB.setVisibility(View.GONE);
actionBar.setCustomView(customActionBarView);
actionBar.setDisplayShowCustomEnabled(true);
}
//listener for back navigation
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.custom_actionbar_back_iv:
Intent recorder = new Intent(ArchiveActivity.this, RecorderActivity.class);
startActivity(recorder);
finish();
break;
}
}
希望这对您有所帮助...