Android应用在Kitkat中无效:

时间:2016-09-10 16:28:38

标签: android android-resources android-navigationview

该应用程序适用于Lollipop,但在KitKat中,它与NavigationDrawer发生了一些错误,我无法解决它。

谢谢:)

错误:



09-10 12:39:40.960 6021-6021/com.fundacaonokia.jiquitaia E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.fundacaonokia.jiquitaia, PID: 6021
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fundacaonokia.jiquitaia/com.fundacaonokia.jiquitaia.MainActivity}: android.view.InflateException: Binary XML file line #50: Error inflating class android.support.design.widget.NavigationView
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
                                                                           at android.app.ActivityThread.access$900(ActivityThread.java:166)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:146)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5511)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        Caused by: android.view.InflateException: Binary XML file line #50: Error inflating class android.support.design.widget.NavigationView
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:626)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                           at com.fundacaonokia.jiquitaia.MainActivity.onCreate(MainActivity.java:28)
                                                                           at android.app.Activity.performCreate(Activity.java:5322)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                           at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:146) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5511) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 
                                                                        Caused by: java.lang.reflect.InvocationTargetException
                                                                           at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                           at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:600)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255) 
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                           at com.fundacaonokia.jiquitaia.MainActivity.onCreate(MainActivity.java:28) 
                                                                           at android.app.Activity.performCreate(Activity.java:5322) 
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                           at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:146) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5511) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 
                                                                        Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020094
                                                                           at android.content.res.Resources.getValue(Resources.java:2354)
                                                                           at android.content.res.Resources.getDrawable(Resources.java:1919)
                                                                           at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
                                                                           at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:146)
                                                                           at android.support.v7.internal.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:422)
                                                                           at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.prepareMenuItems(NavigationMenuPresenter.java:425)
                                                                           at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.notifyDataSetChanged(NavigationMenuPresenter.java:368)
                                                                           at android.support.design.internal.NavigationMenuPresenter.updateMenuView(NavigationMenuPresenter.java:115)
                                                                           at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:229)
                                                                           at android.support.design.widget.NavigationView.<init>(NavigationView.java:164)
                                                                           at android.support.design.widget.NavigationView.<init>(NavigationView.java:92)
                                                                           at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                           at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:600) 
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255) 
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                           at com.fundacaonokia.jiquitaia.MainActivity.onCreate(MainActivity.java:28) 
                                                                           at android.app.Activity.performCreate(Activity.java:5322) 
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                           at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:146) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5511) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 
&#13;
&#13;
&#13;

activity_main.xml中

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.fundacaonokia.jiquitaia.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabGravity="fill"
                app:tabMode="fixed" />

        </android.support.design.widget.AppBarLayout>


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />


    </android.support.design.widget.CoordinatorLayout>

    <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_navigation_drawer"
        app:menu="@menu/navigation_drawer" />

</android.support.v4.widget.DrawerLayout>
&#13;
&#13;
&#13;

MainActivity.java:

&#13;
&#13;
package com.fundacaonokia.jiquitaia;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        assert tabLayout != null;
        tabLayout.setupWithViewPager(viewPager);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        assert drawer != null;
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        assert navigationView != null;
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new HandicraftFragment(), getResources().getString(R.string.fragment_handicraft));
        adapter.addFragment(new EventsFragment(), getResources().getString(R.string.fragment_events));
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        assert drawer != null;
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_about) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        Intent intent = DrawerSettings.setNavigationDrawer(id, this);

        if (intent != null) {
            startActivity(intent);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        assert drawer != null;
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> fragmentList = new ArrayList<>();
        private final List<String> fragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            fragmentList.add(fragment);
            fragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentTitleList.get(position);
        }
    }
}
&#13;
&#13;
&#13;

styles.xml

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <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" />

</resources>
&#13;
&#13;
&#13;

0 个答案:

没有答案