IllegalStateException:找不到方法showPopup(View)

时间:2016-06-10 09:53:52

标签: java android xml android-cardview popupmenu

这是我的卡片视图活动,其中有三个点,就像在工具栏中一样,我想用菜单溢出它但是收到此错误:

  

java.lang.IllegalStateException:找不到父或祖先的方法showPopup(View)for android:onClick属性在视图类android.support.v7.widget.AppCompatImageButton上定义,id为'img_menu'

这是XML

xmlns:card_view="http://schemas.android.com/apk/res-auto"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<android.support.v7.widget.CardView

    android:id="@+id/card_view"

    android:layout_gravity="center"

    android:layout_width="fill_parent"

    android:layout_height="80dp"

    android:layout_margin="5dp"

    card_view:cardCornerRadius="2dp"

    card_view:contentPadding="10dp"

    android:foreground="?android:attr/selectableItemBackground">

    <RelativeLayout

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

        <TextView

            android:id="@+id/textView"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:textStyle="bold"

            android:layout_alignParentTop="true"/>

        <TextView

            android:id="@+id/textView2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginTop="10dp"

            android:layout_below="@+id/textView"/>

        <ImageButton

            android:id="@+id/img_menu"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:background="@drawable/ic_action_navigation_more_vert"

            android:layout_alignParentRight="true"

            android:layout_marginTop="12dp"

            android:onClick="showPopup"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

这是活动

   public class CardViewActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

 private RecyclerView.Adapter mAdapter;

    private RecyclerView.LayoutManager mLayoutManager;

    private static String LOG_TAG = "CardViewActivity";

    ImageButton overflowMenu;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_card_view);

        overflowMenu = (ImageButton) findViewById(R.id.img_menu);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        mRecyclerView.setHasFixedSize(true);


        mLayoutManager = new LinearLayoutManager(this);

        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MyRecyclerViewAdapter(getDataSet());

        mRecyclerView.setAdapter(mAdapter);

        if (getSupportActionBar() != null) {

            getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            getSupportActionBar().setHomeButtonEnabled(true);

        }


    }

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case android.R.id.home:

                onBackPressed();

                return true;



            default:

                return super.onOptionsItemSelected(item);

        }

    }

    public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.card_overflow_menu, popup.getMenu());
    popup.show();

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.one:
                    //Or Some other code you want to put here.. This is just an example.
                    Toast.makeText(getApplicationContext(), " Clicked 1 " + " : " , Toast.LENGTH_LONG).show();
                    break;
                case R.id.two:

                    Toast.makeText(getApplicationContext(), "Clicked 2 " + " : " , Toast.LENGTH_LONG).show();

                    break;

                default:
                    break;
            }

            return true;
        }
    });


    }


 /*   public void showPopup(View v) {

        PopupMenu popup = new PopupMenu(CardViewActivity.this, v);

        MenuInflater inflater = popup.getMenuInflater();


        inflater.inflate(R.menu.card_overflow_menu,popup.getMenu());

        popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) {

        public boolean onMenuClick (MenuItem item){


        }




    popup.show();

}}*/


    /*public void showMenu(View v) {

        PopupMenu popup = new PopupMenu(this, v);

        // This activity implements OnMenuItemClickListener

        popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) 
this);

        popup.inflate(R.menu.card_overflow_menu);

        popup.show();

    }*/


    @Override

    protected void onResume() {

        super.onResume();

        ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new MyRecyclerViewAdapter
                .MyClickListener() {

            @Override

            public void onItemClick(int position, View v) {

                Log.i(LOG_TAG, " Clicked on Item " + position);

            }

        });

    }

    private ArrayList<DataObject> getDataSet() {

        ArrayList results = new ArrayList<DataObject>();

        for (int index = 0; index < 20; index++) {

            DataObject obj = new DataObject("Test " + index,

                    "Doc number " + index);

            results.add(index, obj);

        }

        return results;

    }



}

LogCat

java.lang.IllegalStateException: Could not find method showPopup(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'img_menu'
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                              at android.view.View.performClick(View.java:4802)
                                                                              at android.view.View$PerformClick.run(View.java:20101)
                                                                              at android.os.Handler.handleCallback(Handler.java:810)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                              at android.os.Looper.loop(Looper.java:189)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5532)
                                                                              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:950)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

1 个答案:

答案 0 :(得分:0)

您正在ImageButton上定义onClick事件,而且您也在其上设置onClickListener。

使用

public void showPopup(View v)

代码或

 overflowMenu.setOnClickListener(new View.OnClickListener()

我建议您注释掉overFlowMenu clickListener代码。