我是 Android 新手(第 1 天),今天我学会了使用 viewFlipper
显示图像 (ImageView
)。 ViewFlipper
适用于少量图像(5 个左右)。但是我无法使用它来显示 22 张图像,因为我遇到内存不足错误,并且应用程序强制关闭。因此,我在 StackExchange 中阅读此处,将 ViewFlipper
替换为 ViewPager
,但我收到以下错误。
任何帮助将不胜感激。谢谢。
日志: Pastebin .抱歉,patebin,我无法在这里很好地格式化 logcat。
第二个 Activity
Java 类:
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>
list 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
类。
在 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>
关于java - 安卓.view.InflateException : Binary XML file line #13: Error inflating class ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505974/