我编写了一个简单的应用程序,可以从scrollview中加载多个壁纸(20)并允许用户将它们设置为他/她的背景。它在我的设备上安装并正常工作,但用户在安装后立即报告“强制关闭”错误,这根本不允许他们打开应用程序!我收到的唯一报告的错误是这个OutOfMemory错误,但它非常神秘,我不确定是什么问题!任何指导都将不胜感激,谢谢!
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.jbsoft.BobMarleyExperience/com.jbsoft.BobMarleyExperience.BobMarleyExperi enceActivity}: android.view.InflateException: Binary XML file line #27: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #27: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
at android.app.Activity.setContentView(Activity.java:1657)
at com.jbsoft.BobMarleyExperience.BobMarleyExperienceActivity.onCreate(BobMarleyExperienceActivity.java:33)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 24 more
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715)
at android.content.res.Resources.loadDrawable(Resources.java:1713)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.widget.ImageView.<init>(ImageView.java:122)
at android.widget.ImageView.<init>(ImageView.java:112)
... 27 more
我的onCreate()方法
//Instance vars
int toPhone;
ImageView Display;
Button SetWallPaper;
AdView adview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Inflate the adview
adview = (AdView)this.findViewById(R.id.adView);
//Create new AdRequest object
AdRequest adRequest = new AdRequest();
//Request Ad to be displayed
adview.loadAd(adRequest);
//Display instructions once the activity starts
Toast toast = Toast
.makeText(
getApplicationContext(),
"Scroll through the images below to load the wallpaper, then touch the button to set it!",
Toast.LENGTH_LONG);
toast.show();
//Defining our objects
Display = (ImageView) findViewById(R.id.IVmain);
ImageView image1 = (ImageView) findViewById(R.id.IVone);
ImageView image3 = (ImageView) findViewById(R.id.IVthree);
ImageView image4 = (ImageView) findViewById(R.id.IVfour);
ImageView image5 = (ImageView) findViewById(R.id.IVfive);
ImageView image6 = (ImageView) findViewById(R.id.IVsix);
ImageView image7 = (ImageView) findViewById(R.id.IVseven);
ImageView image8 = (ImageView) findViewById(R.id.IVeight);
ImageView image9 = (ImageView) findViewById(R.id.IVnine);
ImageView image10 = (ImageView) findViewById(R.id.IVten);
ImageView image11 = (ImageView) findViewById(R.id.IVeleven);
ImageView image12 = (ImageView) findViewById(R.id.IVtwelve);
ImageView image13 = (ImageView) findViewById(R.id.IVthirteen);
ImageView image14 = (ImageView) findViewById(R.id.IVfourteen);
ImageView image15 = (ImageView) findViewById(R.id.IVfifteen);
ImageView image16 = (ImageView) findViewById(R.id.IVsixteen);
ImageView image17 = (ImageView) findViewById(R.id.IVseventeen);
ImageView image18 = (ImageView) findViewById(R.id.IVeighteen);
ImageView image19 = (ImageView) findViewById(R.id.IVnineteen);
ImageView image20 = (ImageView) findViewById(R.id.IVtwenty);
//Define Button object and set a listener for it
SetWallPaper = (Button) findViewById(R.id.buttonsetwallpaper);
SetWallPaper.setOnClickListener(this);
//Set listeners for all the images
image1.setOnClickListener(this);
image3.setOnClickListener(this);
Display.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
image7.setOnClickListener(this);
image8.setOnClickListener(this);
image9.setOnClickListener(this);
image10.setOnClickListener(this);
image11.setOnClickListener(this);
image12.setOnClickListener(this);
image13.setOnClickListener(this);
image14.setOnClickListener(this);
image15.setOnClickListener(this);
image16.setOnClickListener(this);
image17.setOnClickListener(this);
image18.setOnClickListener(this);
image19.setOnClickListener(this);
image20.setOnClickListener(this);
}
我的主要布局.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainlayout"
android:background="#808000"
>
<ImageView android:src="@drawable/bob2" android:layout_weight="12" android:id="@+id/IVmain" android:layout_gravity="center" android:layout_width="fill_parent" android:layout_height="200dp"></ImageView>
<HorizontalScrollView android:layout_width="350dp" android:layout_height="100dp" android:layout_gravity="center_horizontal">
<LinearLayout android:orientation="horizontal" android:padding="8dp" android:layout_weight="8" android:layout_height="wrap_content" android:layout_width="wrap_content">
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob1" android:id="@+id/IVone" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob3" android:id="@+id/IVthree" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob4" android:id="@+id/IVfour" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob5" android:id="@+id/IVfive" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob6" android:id="@+id/IVsix" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob7" android:id="@+id/IVseven" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob8" android:id="@+id/IVeight" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob9" android:id="@+id/IVnine" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob10" android:id="@+id/IVten" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#FBB117" android:layout_gravity="center" android:src="@drawable/bob11" android:id="@+id/IVeleven" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView android:layout_width="350dp" android:layout_gravity="center_horizontal" android:layout_height="100dp">
<LinearLayout android:orientation="horizontal" android:padding="8dp" android:layout_height="wrap_content" android:layout_width="wrap_content">
<ImageView android:background="#7E2217" android:layout_gravity="center" android:src="@drawable/bob12" android:id="@+id/IVtwelve" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#7E2217" android:layout_gravity="center" android:src="@drawable/bob13" android:id="@+id/IVthirteen" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#7E2217" android:layout_gravity="center" android:src="@drawable/bob14" android:id="@+id/IVfourteen" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#7E2217" android:layout_gravity="center" android:src="@drawable/bob15" android:id="@+id/IVfifteen" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#7E2217" android:layout_gravity="center" android:src="@drawable/bob16" android:id="@+id/IVsixteen" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#7E2217" android:layout_gravity="center" android:src="@drawable/bob17" android:id="@+id/IVseventeen" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#7E2217" android:layout_gravity="center" android:src="@drawable/bob18" android:id="@+id/IVeighteen" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#7E2217" android:layout_gravity="center" android:src="@drawable/bob19" android:id="@+id/IVnineteen" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
<ImageView android:background="#7E2217" android:layout_gravity="center" android:src="@drawable/bob20" android:id="@+id/IVtwenty" android:padding="3dp" android:layout_height="100dp" android:layout_width="100dp"/>
</LinearLayout>
</HorizontalScrollView>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonsetwallpaper"
android:text="SET AS WALLPAPER" android:drawablePadding="2dp" android:layout_gravity="center_horizontal"></Button>
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ff0000"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
ads:adUnitId="a14e8e1bbcb80eb"/>
</LinearLayout>
答案 0 :(得分:0)
这是非常糟糕的代码片段。您应该知道您的应用程序在设备上只有一小部分内存,因此您无法一次在内存中加载所有Bitmap内容。
GridView
,ListView
而不是拥有大量ImageView
。WALLPAPER
;我推荐Gallery
。