Here's my card view activity where three dots are there as in toolbar and i want to overflow it with menu but getting this error
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'
here's 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> Here's activity
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); } overflowMenu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupMenu popup = new PopupMenu(CardViewActivity.this, overflowMenu); popup.getMenuInflater().inflate(R.menu.card_overflow_menu, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { Toast.makeText(CardViewActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show(); return true; } }); popup.show(); } }); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); return true; case R.id.img_menu: Toast.makeText(getApplicationContext(), "You clicked img_menu", Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } } 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){ switch (item.getItemId()) { case R.id.new_game: Toast.makeText(getApplicationContext(), "You clicked item 1", Toast.LENGTH_SHORT).show(); return true; case R.id.help: Toast.makeText(getApplicationContext(), "You clicked item 2", Toast.LENGTH_SHORT).show(); return true; default: return false; } }} } 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; }
No comments:
Post a Comment