使用ActionBarCompat时出现FileNotFoundException

时间:2013-07-26 09:18:01

标签: android android-studio filenotfoundexception android-actionbar-compat

我正在尝试使用ActionBarCompat,但在模拟器(API 7)中运行应用时,我得到FileNotFoundException

Uncaught handler: thread main exiting due to uncaught exception
  android.view.InflateException: Binary XML file line #2: 
  Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:513)
at com.android.internal.policy.impl.PhoneLayoutInflater
  .onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.cs15.x360a.adapter.NaviMenuAdapter.getView(NaviMenuAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:1274)
at android.widget.ListView.makeAndAddView(ListView.java:1668)
at android.widget.ListView.fillDown(ListView.java:637)
at android.widget.ListView.fillFromTop(ListView.java:694)
at android.widget.ListView.layoutChildren(ListView.java:1507)
at android.widget.AbsListView.onLayout(AbsListView.java:1113)
at android.view.View.layout(View.java:6830)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
at android.view.View.layout(View.java:6830)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:6830)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
at android.view.View.layout(View.java:6830)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:6830)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:6830)
at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller
  .run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.reflect.InvocationTargetException
at android.widget.LinearLayout.<init>(LinearLayout.java:92)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:500)
... 35 more

Caused by: android.content.res.Resources$NotFoundException: 
  File res/drawable-hdpi/ic_logo.png from drawable resource ID #0x0
at android.content.res.Resources.loadDrawable(Resources.java:1710)
at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
at android.view.View.<init>(View.java:1850)
at android.view.View.<init>(View.java:1799)
at android.view.ViewGroup.<init>(ViewGroup.java:284)
... 39 more

Caused by: java.io.FileNotFoundException: res/drawable-hdpi/ic_logo.png
at android.content.res.AssetManager.openNonAssetNative(Native Method)
at android.content.res.AssetManager.openNonAsset(AssetManager.java:390)
at android.content.res.Resources.loadDrawable(Resources.java:1702)
... 43 more

当我在API 14中运行应用程序时,它运行正常。这是我的Gradle构建:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/volley.jar')
    compile files('libs/jsoup-1.7.2.jar')
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.android.support:appcompat-v7:18.0.+'
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

我在FragmentActivity

中将ActionBarActivity更改为BaseActivity
package com.cs15.x360a.app;

import android.content.res.*;
import android.os.*;
import android.support.v4.app.*;
import android.support.v4.widget.*;
import android.support.v7.app.ActionBarActivity;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;
import com.cs15.x360a.adapter.NaviMenuAdapter;

public class BaseActivity extends ActionBarActivity
                          implements OnItemClickListener {
    // Instance variables
    private ActionBarDrawerToggle mDrawerToggle;
    private Fragment mFrag;
    private DrawerLayout mDrawerLayout;
    private ListView mNaviMenu;
    private String[] mNaviMenuTitles;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_logo);

        // Instantiate variables
        mNaviMenuTitles = getResources()
                    .getStringArray(R.array.slide_menu_navigation);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_main);
        mNaviMenu = (ListView) findViewById(R.id.lv_slide_navigation_content);

        // set navigation menu adapter
        mNaviMenu.setAdapter(new NaviMenuAdapter(this, mNaviMenuTitles));
        mNaviMenu.setOnItemClickListener(this);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
                    R.drawable.ic_drawer,
            R.string.navi_open, R.string.navi_close){

                public void onDrawerClosed(View view){
                    // refresh action bar menu
                    invalidateOptionsMenu();
                }

                public void onDrawerOpened(View view){
                    // refresh action bar menu
                    invalidateOptionsMenu();
                }
            };

        // set action bar home icon
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        // display default selected item (navigation drawer)
        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    public void onItemClick(AdapterView<?> parent, View view, 
                                int position, long id) {
        // select item
        selectItem(position);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState){
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items 
        // related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mNaviMenu);
        //menu.findItem(R.id.menu_arcade_list).setVisible(!drawerOpen);
        //menu.findItem(R.id.menu_retail_list).setVisible(!drawerOpen);
        //menu.findItem(R.id.menu_japanese_list).setVisible(!drawerOpen);
        //menu.findItem(R.id.menu_gfwl_list).setVisible(!drawerOpen);
        //menu.findItem(R.id.menu_windows8_list).setVisible(!drawerOpen);
        //menu.findItem(R.id.menu_mobile_list).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // action bar listener
        if (mDrawerToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void selectItem(int position) {
        // Navigation menu item click
        switch (position) {
            case 0:
                // Start LatestAchievements Fragment
                mFrag = new LatestAch();
                break;

            case 1:
                // Start LatestNews Fragment
                mFrag = new LatestNews();
                break;
        }

        // Fragment transaction
        FragmentTransaction mFragTrans = 
            this.getSupportFragmentManager().beginTransaction();
        mFragTrans.replace(R.id.fl_main_content, mFrag).commit();

        // update selected item
        mNaviMenu.setItemChecked(position, true);

        // set action bar title
        getSupportActionBar().setTitle(mNaviMenuTitles[position]);

        // close navigation menu
        mDrawerLayout.closeDrawer(mNaviMenu);
    }
}   

有没有人可以帮我解决这个小问题?

0 个答案:

没有答案