它在我的API 27手机中正常运行,但在API22中崩溃
这是xml文件
<android.support.design.widget.CoordinatorLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ReportsActivity">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/bgBottomNavigation"
app:itemBackground="@color/bgBottomNavigation"
android:foreground="?attr/selectableItemBackground"
app:itemIconTint="@color/nav_color"
app:itemTextColor="@color/nav_color"
app:menu="@menu/navigation" />
这是MainActivity文件
CoordinatorLayout容器;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reports);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
requestPermission();
loadFragment(new BranchVisitFragment());
}
@Override
public void onFragmentInteraction(Uri uri) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.menuLogout:
FirebaseAuth.getInstance().signOut();
finish();
startActivity(new Intent(this, LoginActivity.class));
break;
case R.id.refresh:
Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame_container);
if( f instanceof BranchVisitFragment)
BranchVisitFragment.refresh();
if(f instanceof GroupFragment)
GroupFragment.refresh();
break;
case R.id.excel:
Toast.makeText(this,"Creating the Excel Sheet...",Toast.LENGTH_LONG).show();
CreateExcel.createExcel();
break;
我认为错误是由于此文件引起的,但我不知道如何使两者在API22中都能正常工作
这是nav_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#039BE5" />
<item android:color="#616161" />
</selector>
我已经研究了stackoverflow以寻求可能的解决方案,但是我找不到一个
这是gradle文件
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.itextpdf:itextg:5.5.10'
implementation 'com.android.support:design:28.0.0'
这是错误:
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.BottomNavigationView
at android.view.LayoutInflater.createView(LayoutInflater.java:637)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.aditya.pdf_report.ReportsActivity.onCreate(ReportsActivity.java:59)
at android.app.Activity.performCreate(Activity.java:6093)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1315)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5296)
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:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:611)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.aditya.pdf_report.ReportsActivity.onCreate(ReportsActivity.java:59)
at android.app.Activity.performCreate(Activity.java:6093)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1315)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5296)
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:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070087
at android.content.res.Resources.getValue(Resources.java:1313)
at android.content.res.MiuiResources.getValue(MiuiResources.java:146)
at android.support.v7.content.res.AppCompatResources.isColorInt(AppCompatResources.java:163)
at android.support.v7.content.res.AppCompatResources.inflateColorStateList(AppCompatResources.java:110)
at android.support.v7.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:76)
at android.support.v7.widget.TintTypedArray.getColorStateList(TintTypedArray.java:171)
at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:148)
at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:114)
XML中的第16行
<android.support.design.widget.BottomNavigationView
活动中的第59行
setContentView(R.layout.activity_reports);
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#1F618D</color>
<color name="colorPrimaryDark">#000</color>
<color name="colorAccent">#D81B60</color>
<color name="bgBottomNavigation">#fff</color>
<color name="activated">#039BE5</color>
<color name="notactive">#616161</color>
</resources>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="@+id/menuLogout"
android:title="Logout" />
<item
android:title=""
android:id="@+id/refresh"
android:icon="@drawable/ref"
app:showAsAction="always"/>
<item
android:id="@+id/excel"
android:title="Create Excel"
app:showAsAction="never"/>
答案 0 :(得分:2)
经过12个小时的严格思考和思考,我终于找到了解决方法。
我的代码绝对没错。
它在可绘制文件夹中。
我在v24可绘制文件夹中放置了2个图标PNG,因此它不适用于我检查过的API23和API22。
因此,只需将它们放回Drawable文件夹中,它的工作原理就像是一种魅力。
谢谢大家的帮助。
答案 1 :(得分:0)
底部导航已在api 26中添加 https://developer.android.com/reference/android/support/design/widget/BottomNavigationView
答案 2 :(得分:0)
我遇到了同样的问题,并通过从底部导航视图xml中删除样式属性来解决。
style="@style/Widget.MaterialComponents.BottomNavigationView"