Android App not working in 4.0 but working on 5+ E

2019-02-21 05:19发布

Once run my app in 5.0+ versions it working nicely. but for lower version it gives me errors and not opening up.

This is the error I'm getting:

Unable to start activity ComponentInfo{lk.myoffers.app.myofferslk/lk.myoffers.app.myofferslk.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.widget.NavigationView

  1. Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.widget.NavigationView
  2. Caused by: java.lang.reflect.InvocationTargetException
  3. Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020052
  4. at lk.myoffers.app.myofferslk.MainActivity.onCreate(MainActivity.java:84) 

I have tried every thing on Stack Overflow but the issue is still remaining.

I'm new to android, so what could be the issue?

setContentView(R.layout.activity_main); is in line 84.

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener{


public static int pageCount = 1;

private int totalLoadedCount;
private AdListAdapter adapter;

TextView txtUserName;
Button btnReconnect;

private static String LIST_INSTANCE_STATE = "items";
private ArrayList<AdList> adLists = new ArrayList<AdList>();

private ArrayList<MasterCategory> mstCategory = new ArrayList<MasterCategory>();
private MasterCategoryAdapter catAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    totalLoadedCount = 0;

My activity_main:

<?xml version="1.0" encoding="utf-8"?>

<include
    layout="@layout/app_bar_main"
    android:id="@+id/aa"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"
    />

My Android manifest file:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme1"
        android:configChanges="orientation|keyboardHidden|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and for the style.xml i have following code lines

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme1" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:button">@color/colorButton</item>
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#673AB7</item>
    <item name="colorPrimaryDark">#512DA8</item>
    <item name="colorAccent">#00BCD4</item>
</style>

<style name="MIP_theme" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

I have tried every thing, but it's not working on Android lower than 5 Gradle version set

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'

The minimum version is 23.

Here is my build.gradle

android {
compileSdkVersion 23
buildToolsVersion "24.0.1"
defaultConfig {
    applicationId "lk.myoffers.app.myofferslk"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'com.github.darsh2:MultipleImageSelect:v0.0.4'
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support:cardview-v7:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
}

1条回答
对你真心纯属浪费
2楼-- · 2019-02-21 05:45

Finally after many days and after doing many debugs thanks to #0X0nosugar I have checked the navigationview by removing

  • app:headerLayout="@layout/nav_header_main"

  • app:menu="@menu/activity_main_drawer

these two line separately and check the android monitor for errors

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
/>

removing app:headerLayout="@layout/nav_header_main" doesn't make any changes gives me the same error

But after removing app:menu="@menu/activity_main_drawer" app works fine and load the left side navigation with no menu item. so as you can see in my error list it says Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020052

So I checked by removing custom vector icons in menu item I placed in my activity_main_drawer in res->menu . Its now working fine. problem with a one

<item
            android:id="@+id/ic_menu_logout"
            android:icon="@drawable/change_this_icon_with_default_icon_comes_in_android"
            android:title="Logout"
            />
        <item

now its working fine.. Everyone who get this issue check your console. if its says "ResourceNotFoundException" this Error inflating class android.support.design.widget.NavigationView probably rising from one of the menu icon.. Finally Works for me! Thanks everyone for your great support

查看更多
登录 后发表回答