引起:android.view.InflateException:二进制XML文件行#15:错误膨胀类android.widget.ImageView

时间:2015-11-05 19:37:50

标签: android android-studio

我已经查看了与我的类似错误的其他问题,但他们似乎有点不同。

在logcat中我收到此错误。我不知道如何解决这个问题。

11-05 13:10:04.211 30912-30912/? E/AndroidRuntime: FATAL EXCEPTION: main
11-05 13:10:04.211 30912-30912/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesgn.app/com.thesgn.app.activity.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class android.widget.ImageView
11-05 13:10:04.211 30912-30912/? E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class android.widget.ImageView
11-05 13:10:04.211 30912-30912/? E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
11-05 13:10:04.211 30912-30912/? E/AndroidRuntime:  Caused by: android.content.res.Resources$NotFoundException: File res/layout/placeholder_offline.xml from drawable resource ID #0x7f03001b
11-05 13:10:04.211 30912-30912/? E/AndroidRuntime:  Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag ScrollView

这是我的MainActivity

package com.thesgn.app.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.thesgn.app.R;
import com.thesgn.app.WebViewAppApplication;
import com.thesgn.app.adapter.DrawerAdapter;
import com.thesgn.app.fragment.MainFragment;


public class MainActivity extends ActionBarActivity
{
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private ListView mDrawerListView;

    private CharSequence mTitle;
    private CharSequence mDrawerTitle;
    private String[] mTitles;


    public static Intent newIntent(Context context)
    {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return intent;
    }


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

        // init analytics tracker
        ((WebViewAppApplication) getApplication()).getTracker();
    }


    @Override
    public void onStart()
    {
        super.onStart();

        // analytics
        GoogleAnalytics.getInstance(this).reportActivityStart(this);
    }


    @Override
    public void onResume()
    {
        super.onResume();
    }


    @Override
    public void onPause()
    {
        super.onPause();
    }


    @Override
    public void onStop()
    {
        super.onStop();

        // analytics
        GoogleAnalytics.getInstance(this).reportActivityStop(this);
    }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // action bar menu
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // open or close the drawer if home button is pressed
        if(mDrawerToggle.onOptionsItemSelected(item))
        {
            return true;
        }

        // action bar menu behaviour
        switch(item.getItemId())
        {
            case android.R.id.home:
                Intent intent = MainActivity.newIntent(this);
                startActivity(intent);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }


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


    @Override
    public void onConfigurationChanged(Configuration newConfiguration)
    {
        super.onConfigurationChanged(newConfiguration);
        mDrawerToggle.onConfigurationChanged(newConfiguration);
    }


    @Override
    public void setTitle(CharSequence title)
    {
        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    }


    private void setupActionBar()
    {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar bar = getSupportActionBar();
        bar.setDisplayUseLogoEnabled(false);
        bar.setDisplayShowTitleEnabled(true);
        bar.setDisplayShowHomeEnabled(true);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setHomeButtonEnabled(true);
    }


    private void setupDrawer(Bundle savedInstanceState)
    {
        mTitle = getTitle();
        mDrawerTitle = getTitle();

        // title list
        mTitles = getResources().getStringArray(R.array.navigation_title_list);

        // icon list
        TypedArray iconTypedArray = getResources().obtainTypedArray(R.array.navigation_icon_list);
        Integer[] icons = new Integer[iconTypedArray.length()];
        for(int i=0; i<iconTypedArray.length(); i++)
        {
            icons[i] = iconTypedArray.getResourceId(i, -1);
        }
        iconTypedArray.recycle();

        // reference
        mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main_layout);
        mDrawerListView = (ListView) findViewById(R.id.activity_main_drawer);

        // set drawer
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        mDrawerListView.setAdapter(new DrawerAdapter(this, mTitles, icons));
        mDrawerListView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View clickedView, int position, long id)
            {
                selectDrawerItem(position, false);
            }
        });
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close)
        {
            @Override
            public void onDrawerClosed(View view)
            {
                getSupportActionBar().setTitle(mTitle);
                supportInvalidateOptionsMenu();
            }

            @Override
            public void onDrawerOpened(View drawerView)
            {
                getSupportActionBar().setTitle(mDrawerTitle);
                supportInvalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        // show initial fragment
        if(savedInstanceState == null)
        {
            selectDrawerItem(0, true);
        }
    }


    private void selectDrawerItem(int position, boolean init)
    {
        String[] urlList = getResources().getStringArray(R.array.navigation_url_list);
        String[] shareList = getResources().getStringArray(R.array.navigation_share_list);

        Fragment fragment = MainFragment.newInstance(urlList[position], shareList[position]);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.activity_main_container, fragment).commitAllowingStateLoss();

        mDrawerListView.setItemChecked(position, true);
        if(!init) setTitle(mTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerListView);
    }
}

任何帮助都将不胜感激。

placerholder_offline.xml

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:padding="@dimen/global_keyline_s">

        <ImageView
            android:layout_width="@dimen/placeholder_image_size"
            android:layout_height="@dimen/placeholder_image_size"
            android:layout_marginBottom="@dimen/global_spacing_m"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@layout/placeholder_offline" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textAppearance="@style/TextAppearance.WebViewApp.Subhead"
            android:text="@string/placeholder_offline" />

    </LinearLayout>
</ScrollView>

drawer_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/global_spacing_l"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:background="?attr/drawerItemBackground">

    <ImageView
        android:id="@+id/drawer_item_icon"
        android:layout_width="@dimen/global_spacing_m"
        android:layout_height="@dimen/global_spacing_m"
        android:layout_marginLeft="@dimen/global_keyline_s"
        android:layout_marginRight="@dimen/global_spacing_xs"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />

    <TextView
        android:id="@+id/drawer_item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/global_keyline_s"
        android:textAppearance="@style/TextAppearance.WebViewApp.Body1" />

</LinearLayout>

1 个答案:

答案 0 :(得分:3)

正如异常所述,问题是由您的placeholder_offline.xml drawable引起的,其中显然包含<ScrollView>标记。

您无法在可绘制的XML中使用<ScrollView>(或任何其他View标签) - 您无意中将布局文件放在可绘制文件夹中,或者您需要重新评估您使用该可绘制文件的内容。

在您的情况下,您的placeholder_offline布局包含以下内容:

<ImageView
    android:layout_width="@dimen/placeholder_image_size"
    android:layout_height="@dimen/placeholder_image_size"
    android:layout_marginBottom="@dimen/global_spacing_m"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:src="@layout/placeholder_offline" />

android:src="@layout/placeholder_offline"属性无效 - android:src需要引用drawable,而不是布局。