尚未显示操作栏自定义视图的 onclicklistener

onclicklistener for actionbar customview not already showed

我的操作栏有两种不同的布局,每一种都由另一种触发。问题是我只能管理打开布局中的按钮,而不是当我尝试将 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.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.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)

您正在为尚未添加到 ViewGroupView 呼叫 findViewById()。您需要将处理后退按钮按下的代码移动到搜索按钮的点击侦听器。

替换

//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);
    }
});

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);
        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);
            }
        });
    }
});