我的操作栏有两种不同的布局,每种布局都由另一种触发。问题是我只能在开放布局中管理按钮,而是当我尝试将onclicklistener与视图中的按钮关联时,我的应用程序崩溃了。
MainActivity:
public class MainActivity extends ActionBarActivity {
ActionBar action = getSupportActionBar();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
action = getSupportActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#33000000"));
action.setBackgroundDrawable(colorDrawable);
action.setDisplayShowTitleEnabled(false);
action.setDisplayShowCustomEnabled(true);
action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
action.setCustomView(R.layout.search_button);
//SEARCH BUTTON
ImageButton searchButton = (ImageButton)findViewById(R.id.search_button);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(),"clicked",Toast.LENGTH_LONG).show();
action.setCustomView(R.layout.search_form);
}
});
/* //BACK BUTTON
ImageButton backButton = (ImageButton)findViewById(R.id.back_button);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(),"clicked",Toast.LENGTH_LONG).show();
//action.setCustomView(R.layout.search_button);
}
});*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
search_form:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right|center_vertical">
<ImageButton
android:id="@+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/_back"
android:clickable="true"
android:background="#00000000"
android:paddingRight="8dp"
android:layout_gravity="center_vertical"/>
<EditText
android:id="@+id/search_form"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#ffffff"
android:hint="search"
android:textColorHint="#ffffff"
android:textCursorDrawable="@drawable/pipe"
android:background="#00000000"
/>
</LinearLayout>
search_button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right|center_vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="le ultime novità"
android:layout_gravity="center_vertical"
android:textSize="22sp"
android:paddingRight="12dp"/>
<ImageButton
android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/_search"
android:clickable="true"
android:background="#00000000"
android:paddingRight="8dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
logcat:
03-08 11:45:35.149 20026-20026/com.example.actionbar E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.actionbar, PID: 20026
java.lang.IllegalStateException: Could not find a method previous(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageButton with id 'back_button'
at android.view.View$1.onClick(View.java:3994)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoSuchMethodException: previous [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:3987)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
答案 0 :(得分:0)
您正在为findViewById()
调用尚未添加到View
的{{1}}。您需要将用于处理后退按钮的代码移动到搜索按钮的单击侦听器。
替换
ViewGroup
与
//SEARCH BUTTON
ImageButton searchButton = (ImageButton)findViewById(R.id.search_button);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(),"clicked",Toast.LENGTH_LONG).show();
action.setCustomView(R.layout.search_form);
}
});
//BACK BUTTON
ImageButton backButton = (ImageButton)findViewById(R.id.back_button);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(),"clicked",Toast.LENGTH_LONG).show();
action.setCustomView(R.layout.search_button);
}
});