【问题标题】:android.view.InflateException: Binary XML file line #13: Error inflating class ViewPagerandroid.view.InflateException: Binary XML file line #13: Error inflating class ViewPager
【发布时间】:2015-04-14 20:54:35
【问题描述】:

我是 Android 新手(第 1 天),今天我被教导使用 viewFlipper 显示图像 (ImageView)。 ViewFlipper 适用于少量图像(5 个左右)。但是我无法使用它来显示 22 张图像,因为我遇到内存不足错误,并且应用程序强制关闭。因此,我在 StackExchange 中阅读此处以将 ViewFlipper 替换为 ViewPager,然后出现以下错误。

任何帮助将不胜感激。谢谢。

Logcat: Pastebin。抱歉,patbin,我在这里无法很好地格式化 logcat。

第二个ActivityJava类:

package com.droidrish.www.rishabhtatiraju;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
public class photoActivity extends Activity{
ViewPager vf;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photography);
    vf = (ViewPager)findViewById(R.id.viewpager);
}   

第二个Activity 布局:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true">
<ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p1"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p2"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p3"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p4"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p5"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p6"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p7"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p8"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p9"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p10"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p11"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p12"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p13"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p14"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p15"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p16"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p17"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p18"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p19"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p20"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p21"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/p22"/>
    </ViewPager>
</LinearLayout>

清单 XML:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".photoActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.droidrish.www.rishabhtatiraju.photoActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>
</manifest>

【问题讨论】:

    标签: java android android-layout android-intent android-studio


    【解决方案1】:

    android.view.InflateException:二进制 XML 文件第 13 行:错误 膨胀类 ViewPager

    因为在 xml 中使用 ViewPager,但在 Activity 代码中尝试将 ViewPager 转换为 android.support.v4.view.ViewPager 类。

    在 xml 中使用 android.support.v4.view.ViewPager 而不是 ViewPager

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

    【讨论】:

    • 是的,ViewPager 不再有任何问题,谢谢。但我现在有一组新的错误:paste.ubuntu.com/10208639
    • 请注意,当我点击导致第二个活动的图像时,应用程序强制关闭并在 logcat 中显示这些错误。
    • @RishabhTatiraju:Out of memory on a 10469392-byte allocation. 可能是由于图像大小。在谷歌上搜索你会得到很多关于同样问题的帖子。用于测试使用小尺寸图片
    • 我也应该发布主要活动的代码及其xml吗?
    • @RishabhTatiraju:我的建议是通过接受这个问题作为答案来完成这个问题,这个答案可以帮助您解决问题中发布的问题。对于下一个问题,而不是编辑原始问题,使用代码的相关部分和错误日志提出一个新问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2015-08-21
    • 2012-02-09
    • 2012-03-04
    相关资源
    最近更新 更多