android.view.InflateException:二进制 XML 文件第 13 行:膨胀 class ViewPager 时出错
android.view.InflateException: Binary XML file line #13: Error inflating class ViewPager
我是 Android(第 1 天)的新手,今天我学会了使用 viewFlipper
显示图像 (ImageView
)。 ViewFlipper
适用于少量图像(5 个左右)。但是我无法使用它来显示 22 张图像,因为我遇到内存不足错误,并且应用程序强制关闭。所以我在 StackExchange 中阅读此处将 ViewFlipper
替换为 ViewPager
,但出现以下错误。
如有任何帮助,我们将不胜感激。谢谢。
Logcat:
Pastebin。抱歉,patebin,我无法在此处格式化 logcat。
第二个Activity
JavaClass:
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>
android.view.InflateException: Binary XML file line #13: Error
inflating class ViewPager
因为在 xml 中使用 ViewPager
但在 Activity 代码中尝试将 ViewPager
转换为 android.support.v4.view.ViewPager
class.
在 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>
我是 Android(第 1 天)的新手,今天我学会了使用 viewFlipper
显示图像 (ImageView
)。 ViewFlipper
适用于少量图像(5 个左右)。但是我无法使用它来显示 22 张图像,因为我遇到内存不足错误,并且应用程序强制关闭。所以我在 StackExchange 中阅读此处将 ViewFlipper
替换为 ViewPager
,但出现以下错误。
如有任何帮助,我们将不胜感激。谢谢。
Logcat: Pastebin。抱歉,patebin,我无法在此处格式化 logcat。
第二个Activity
JavaClass:
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>
android.view.InflateException: Binary XML file line #13: Error inflating class ViewPager
因为在 xml 中使用 ViewPager
但在 Activity 代码中尝试将 ViewPager
转换为 android.support.v4.view.ViewPager
class.
在 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>