Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Toolbar menu causes my application to crash when opened without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
I’m having trouble with the menu in my toolbar. The project builds but when the drop down menu is pressed in the top corner,the application crashes. At first i thought it may have been a problem inflating the menu itself but i can’t locate the line that is causing this error.
MainActivity.java:
public class MainActivity extends AppCompatActivity { CircleImageView usrImg; TextView usrName; FirebaseUser firebaseUser; DatabaseReference ref; @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //<-------TOOLBAR LAYOUT-----------> Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setTitle(""); } //MENU CONTROLS @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()){ case R.id.signOut: FirebaseAuth.getInstance().signOut(); startActivity(new Intent(MainActivity.this,LoginActivity.class)); finish(); return true; } return false; } }
MainActivity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/brLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimaryDark" android:theme="@style/Theme.AppCompat.Light.DarkActionBar" app:popupTheme="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu"> <de.hdodenhof.circleimageview.CircleImageView android:layout_width="40dp" android:layout_height="40dp" android:id="@+id/imgProfile"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txtProfile" android:text="USERNAME" android:textColor="#FEFCFB" android:layout_marginLeft="25dp" android:layout_marginStart="25dp" /> </androidx.appcompat.widget.Toolbar> </com.google.android.material.appbar.AppBarLayout> </RelativeLayout>
menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/inbox" android:title="Messages" app:showAsAction="never"/> <item android:id="@+id/signOut" android:title="Sign Out" app:showAsAction="never"/> </menu>
ERROR:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mseabraham.finalyearapp, PID: 26662 android.view.InflateException: Binary XML file line #51: Binary XML file line #51: Error inflating class TextView Caused by: android.view.InflateException: Binary XML file line #51: Error inflating class TextView Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1} at android.content.res.TypedArray.getColor(TypedArray.java:477) at android.widget.TextView.readTextAppearance(TextView.java:3539) at android.widget.TextView.<init>(TextView.java:924) at android.widget.TextView.<init>(TextView.java:869) at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:99) at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:95) at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:182) at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103) at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407) at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at androidx.appcompat.view.menu.MenuAdapter.getView(MenuAdapter.java:94) at androidx.appcompat.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:161) at androidx.appcompat.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:174) at androidx.appcompat.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:208) at androidx.appcompat.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290) at androidx.appcompat.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:177) at androidx.appcompat.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:797) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Answer
Try to change the line of code given below. Provide any custom theme or any other theme and if you don’t want any theme you can also delete popupTheme it will work fine
app:popupTheme="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu"
We are here to answer your question about Toolbar menu causes my application to crash when opened - If you find the proper solution, please don't forgot to share this with your team members.