工具栏菜单导致我的应用程序在打开时崩溃
Toolbar menu causes my application to crash when opened
我在使用工具栏中的菜单时遇到问题。该项目已构建,但是当按下右上角的下拉菜单时,应用程序崩溃了。起初我以为菜单本身膨胀可能是个问题,但我找不到导致此错误的行。
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>
错误:
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)
尝试更改下面给出的代码行。提供任何自定义主题或任何其他主题,如果您不想要任何主题,您也可以删除 popupTheme 它将正常工作
app:popupTheme="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu"
我在使用工具栏中的菜单时遇到问题。该项目已构建,但是当按下右上角的下拉菜单时,应用程序崩溃了。起初我以为菜单本身膨胀可能是个问题,但我找不到导致此错误的行。
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>
错误:
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)
尝试更改下面给出的代码行。提供任何自定义主题或任何其他主题,如果您不想要任何主题,您也可以删除 popupTheme 它将正常工作
app:popupTheme="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu"