当我在Nexus 5上运行此代码时,它在启动时崩溃,我不知道为什么。
以下是预览窗口的屏幕截图:
我的代码:
MainActivity.java:
package com.example.coolfreewallpapers;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
ImageView display;
int toPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// check if next two lines are necessary
requestWindowFeature(Window.FEATURE_NO_TITLE); // gets rid of app title
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (android.os.Build.VERSION.SDK_INT>=11) {
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
}
setContentView(R.layout.wallpaper);
toPhone = R.drawable.one;
display = (ImageView) findViewById(R.id.IVdisplay);
ImageView one = (ImageView) findViewById(R.id.IVimage1);
ImageView two = (ImageView) findViewById(R.id.IVimage2);
ImageView three = (ImageView) findViewById(R.id.IVimage3);
ImageView four = (ImageView) findViewById(R.id.IVimage4);
ImageView five = (ImageView) findViewById(R.id.IVimage5);
ImageView six = (ImageView) findViewById(R.id.IVimage6);
ImageView seven = (ImageView) findViewById(R.id.IVimage7);
ImageView eight = (ImageView) findViewById(R.id.IVimage8);
ImageView nine = (ImageView) findViewById(R.id.IVimage9);
ImageView ten = (ImageView) findViewById(R.id.IVimage10);
ImageView eleven = (ImageView) findViewById(R.id.IVimage11);
ImageView twelve = (ImageView) findViewById(R.id.IVimage12);
ImageView thirteen = (ImageView) findViewById(R.id.IVimage13);
ImageView fourteen = (ImageView) findViewById(R.id.IVimage14);
ImageView fifteen = (ImageView) findViewById(R.id.IVimage15);
ImageView sixteen = (ImageView) findViewById(R.id.IVimage16);
ImageView seventeen = (ImageView) findViewById(R.id.IVimage17);
ImageView eighteen = (ImageView) findViewById(R.id.IVimage18);
ImageView nineteen = (ImageView) findViewById(R.id.IVimage19);
ImageView twenty = (ImageView) findViewById(R.id.IVimage20);
ImageView twentyone = (ImageView) findViewById(R.id.IVimage21);
ImageView twentytwo = (ImageView) findViewById(R.id.IVimage22);
ImageView twentythree = (ImageView) findViewById(R.id.IVimage23);
ImageView twentyfour = (ImageView) findViewById(R.id.IVimage24);
ImageView twentyfive = (ImageView) findViewById(R.id.IVimage25);
ImageView twentysix = (ImageView) findViewById(R.id.IVimage26);
ImageView twentyseven = (ImageView) findViewById(R.id.IVimage27);
ImageView twentyeight = (ImageView) findViewById(R.id.IVimage28);
ImageView twentynine = (ImageView) findViewById(R.id.IVimage29);
ImageView thirty = (ImageView) findViewById(R.id.IVimage30);
Button setWall = (Button) findViewById(R.id.BsetWallpaper);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
ten.setOnClickListener(this);
eleven.setOnClickListener(this);
twelve.setOnClickListener(this);
thirteen.setOnClickListener(this);
fourteen.setOnClickListener(this);
fifteen.setOnClickListener(this);
sixteen.setOnClickListener(this);
seventeen.setOnClickListener(this);
eighteen.setOnClickListener(this);
nineteen.setOnClickListener(this);
twenty.setOnClickListener(this);
twentyone.setOnClickListener(this);
twentytwo.setOnClickListener(this);
twentythree.setOnClickListener(this);
twentyfour.setOnClickListener(this);
twentyfive.setOnClickListener(this);
twentysix.setOnClickListener(this);
twentyseven.setOnClickListener(this);
twentyeight.setOnClickListener(this);
twentynine.setOnClickListener(this);
thirty.setOnClickListener(this);
setWall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.IVimage1:
display.setImageResource(R.drawable.one);
toPhone = R.drawable.one;
break;
case R.id.IVimage2:
display.setImageResource(R.drawable.two);
toPhone = R.drawable.two;
break;
case R.id.IVimage3:
display.setImageResource(R.drawable.three);
toPhone = R.drawable.three;
break;
case R.id.IVimage4:
display.setImageResource(R.drawable.four);
toPhone = R.drawable.four;
break;
case R.id.IVimage5:
display.setImageResource(R.drawable.five);
toPhone = R.drawable.five;
break;
case R.id.IVimage6:
display.setImageResource(R.drawable.six);
toPhone = R.drawable.six;
break;
case R.id.IVimage7:
display.setImageResource(R.drawable.seven);
toPhone = R.drawable.seven;
break;
case R.id.IVimage8:
display.setImageResource(R.drawable.eight);
toPhone = R.drawable.eight;
break;
case R.id.IVimage9:
display.setImageResource(R.drawable.nine);
toPhone = R.drawable.nine;
break;
case R.id.IVimage10:
display.setImageResource(R.drawable.ten);
toPhone = R.drawable.ten;
break;
case R.id.IVimage11:
display.setImageResource(R.drawable.eleven);
toPhone = R.drawable.eleven;
break;
case R.id.IVimage12:
display.setImageResource(R.drawable.twelve);
toPhone = R.drawable.twelve;
break;
case R.id.IVimage13:
display.setImageResource(R.drawable.thirteen);
toPhone = R.drawable.thirteen;
break;
case R.id.IVimage14:
display.setImageResource(R.drawable.fourteen);
toPhone = R.drawable.fourteen;
break;
case R.id.IVimage15:
display.setImageResource(R.drawable.fifteen);
toPhone = R.drawable.fifteen;
break;
case R.id.IVimage16:
display.setImageResource(R.drawable.sixteen);
toPhone = R.drawable.sixteen;
break;
case R.id.IVimage17:
display.setImageResource(R.drawable.seventeen);
toPhone = R.drawable.seventeen;
break;
case R.id.IVimage18:
display.setImageResource(R.drawable.eighteen);
toPhone = R.drawable.eighteen;
break;
case R.id.IVimage19:
display.setImageResource(R.drawable.nineteen);
toPhone = R.drawable.nineteen;
break;
case R.id.IVimage20:
display.setImageResource(R.drawable.twenty);
toPhone = R.drawable.twenty;
break;
case R.id.IVimage21:
display.setImageResource(R.drawable.twentyone);
toPhone = R.drawable.twentyone;
break;
case R.id.IVimage22:
display.setImageResource(R.drawable.twentytwo);
toPhone = R.drawable.twentytwo;
break;
case R.id.IVimage23:
display.setImageResource(R.drawable.twentythree);
toPhone = R.drawable.twentythree;
break;
case R.id.IVimage24:
display.setImageResource(R.drawable.twentyfour);
toPhone = R.drawable.twentyfour;
break;
case R.id.IVimage25:
display.setImageResource(R.drawable.twentyfive);
toPhone = R.drawable.twentyfive;
break;
case R.id.IVimage26:
display.setImageResource(R.drawable.twentysix);
toPhone = R.drawable.twentysix;
break;
case R.id.IVimage27:
display.setImageResource(R.drawable.twentyseven);
toPhone = R.drawable.twentyseven;
break;
case R.id.IVimage28:
display.setImageResource(R.drawable.twentyeight);
toPhone = R.drawable.twentyeight;
break;
case R.id.IVimage29:
display.setImageResource(R.drawable.twentynine);
toPhone = R.drawable.twentynine;
break;
case R.id.IVimage30:
display.setImageResource(R.drawable.thirty);
toPhone = R.drawable.thirty;
break;
// create method that means if low quality image is selected,
high quality image is set as wallpaper
case R.id.BsetWallpaper:
//how to downsample
InputStream yeaaaa = getResources().openRawResource(toPhone);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; // is half the size, 4 a quarter etc..
Bitmap whatever = BitmapFactory.decodeStream(yeaaaa, null, options);
try {
getApplicationContext().setWallpaper(whatever);
}catch(IOException e){
e.printStackTrace();
}
break;
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.coolfreewallpapers"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/MyTheme"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
wallpaper.xml - the only wallpaper/layout file I am using
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView android:src="@drawable/one" android:id="@+id/IVdisplay"
android:layout_width="350dp" android:layout_height="350dp"
android:contentDescription="@string/imageViewDescription"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:cropToPadding="true"
android:background="@drawable/border"
/>
<!-- not sure if adjustviewBounds is needed -->
<!-- style="@style/myImageView"
-->
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView android:src="@drawable/one" android:id="@+id/IVimage1"
android:layout_width="175dp" android:layout_height="170dp"
android:contentDescription="@string/imageViewDescription"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:cropToPadding="true"
android:background="@drawable/border_images"
android:padding="1dp"
/>
<ImageView android:src="@drawable/two" android:id="@+id/IVimage2"
android:layout_width="175dp" android:layout_height="170dp"
android:contentDescription="@string/imageViewDescription"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:cropToPadding="true"
android:background="@drawable/border_images"
android:padding="1dp"
/>
<ImageView android:src="@drawable/three" android:id="@+id/IVimage3"
android:layout_width="175dp" android:layout_height="170dp"
android:contentDescription="@string/imageViewDescription"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:cropToPadding="true"
android:background="@drawable/border_images"
android:padding="1dp"
/>
<ImageView android:src="@drawable/four" android:id="@+id/IVimage4"
android:layout_width="175dp" android:layout_height="170dp"
android:contentDescription="@string/imageViewDescription"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:cropToPadding="true"
android:background="@drawable/border_images"
android:padding="1dp"
/>
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="@+id/BsetWallpaper"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginLeft="2.5dp"
android:layout_marginRight="2.5dp"
android:text="@string/wallpaperbuttontext" />
</LinearLayout>
Styles:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go
here. -->
</style>
<!-- Holo no title bar theme -->
<style parent="android:Theme.Holo" name="MyTheme">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
我已经尝试了Project&gt;清理并重启了eclipse几次,但到目前为止还没有帮助
我已经遗漏了25个与我发布的相同的其他ImageView,除了数量不断增加。但我确信这些都是正确的。任何建议将不胜感激
编辑:
以下是Eclipse的LogCat:
07-30 05:49:51.814: E/AndroidRuntime(11305): FATAL EXCEPTION: main
07-30 05:49:51.814: E/AndroidRuntime(11305): Process: com.example.coolfreewallpapers, PID: 11305
07-30 05:49:51.814: E/AndroidRuntime(11305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.coolfreewallpapers/com.example.coolfreewallpapers.MainActivity}: android.view.InflateException: Binary XML file line #233: Error inflating class <unknown>
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.os.Handler.dispatchMessage(Handler.java:102)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.os.Looper.loop(Looper.java:136)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-30 05:49:51.814: E/AndroidRuntime(11305): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 05:49:51.814: E/AndroidRuntime(11305): at java.lang.reflect.Method.invoke(Method.java:515)
07-30 05:49:51.814: E/AndroidRuntime(11305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-30 05:49:51.814: E/AndroidRuntime(11305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-30 05:49:51.814: E/AndroidRuntime(11305): at dalvik.system.NativeStart.main(Native Method)
07-30 05:49:51.814: E/AndroidRuntime(11305): Caused by: android.view.InflateException: Binary XML file line #233: Error inflating class <unknown>
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
07-30 05:49:51.814: E/AndroidRuntime(11305): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-30 05:49:51.814: E/AndroidRuntime(11305): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.app.Activity.setContentView(Activity.java:1929)
07-30 05:49:51.814: E/AndroidRuntime(11305): at com.example.coolfreewallpapers.MainActivity.onCreate(MainActivity.java:38)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.app.Activity.performCreate(Activity.java:5231)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-30 05:49:51.814: E/AndroidRuntime(11305): ... 11 more
07-30 05:49:51.814: E/AndroidRuntime(11305): Caused by: java.lang.reflect.InvocationTargetException
07-30 05:49:51.814: E/AndroidRuntime(11305): at java.lang.reflect.Constructor.constructNative(Native Method)
07-30 05:49:51.814: E/AndroidRuntime(11305): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
07-30 05:49:51.814: E/AndroidRuntime(11305): ... 26 more
07-30 05:49:51.814: E/AndroidRuntime(11305): Caused by: java.lang.OutOfMemoryError
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.content.res.Resources.loadDrawable(Resources.java:2110)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.widget.ImageView.<init>(ImageView.java:129)
07-30 05:49:51.814: E/AndroidRuntime(11305): at android.widget.ImageView.<init>(ImageView.java:119)
07-30 05:49:51.814: E/AndroidRuntime(11305): ... 29 more
这有助于解释什么吗?
答案 0 :(得分:0)
听起来您的图片已损坏。从drawable文件夹中删除它并将其替换为新文件夹。
之前我有过类似的问题。这是由于图像文件损坏。我不知道为什么android报告找不到ImageView类,我也没有想过挖掘源代码来查找它,但这对我来说是什么。
答案 1 :(得分:0)
超出内存范围的异常
这是内存不足的例外
删除所有xml版本的android:background =“@ drawable /”和android:src =“@ drawable /”的ImageView
或
减小可绘制图像的大小