我正在尝试制作一个设置用户壁纸的应用程序,但每次单击“设置壁纸”按钮时它都会崩溃。我正在使用 640X360px 的图像,每个图像 100-200kb
我的代码是:
package basics.noaml.com.basics;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by Noam on 29/11/2014.
*/
public class TutorialThree extends Activity implements View.OnClickListener {
ImageView display;
int current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
actions();
}
public void actions(){
current = R.id.IVimage1;
display = (ImageView) findViewById(R.id.IVdisplay);
ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
ImageView image6 = (ImageView) findViewById(R.id.IVimage6);
ImageView image7 = (ImageView) findViewById(R.id.IVimage7);
Button setWall = (Button) findViewById(R.id.bSetWallpaper);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
image7.setOnClickListener(this);
setWall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.IVimage1:
display.setImageResource(R.drawable.bg_1);
current = R.id.IVimage1;
break;
case R.id.IVimage2:
display.setImageResource(R.drawable.bg_2);
current = R.id.IVimage2;
break;
case R.id.IVimage3:
display.setImageResource(R.drawable.bg_3);
current = R.id.IVimage3;
break;
case R.id.IVimage4:
display.setImageResource(R.drawable.bg_4);
current = R.id.IVimage4;
break;
case R.id.IVimage5:
display.setImageResource(R.drawable.bg_5);
current = R.id.IVimage5;
break;
case R.id.IVimage6:
display.setImageResource(R.drawable.bg_6);
current = R.id.IVimage6;
break;
case R.id.IVimage7:
display.setImageResource(R.drawable.bg_7);
current = R.id.IVimage7;
break;
case R.id.bSetWallpaper:
InputStream yeaaaa = getResources().openRawResource(current);
Bitmap wallpaper = BitmapFactory.decodeStream(yeaaaa);
WallpaperManager myWallpaperManager=WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(wallpaper);
}
catch (IOException e) {
Toast.makeText(TutorialThree.this, "Ooops, couldn't set the wallpaper", Toast.LENGTH_LONG).show();
}
break;
}
}
}
和布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/IVdisplay"
android:layout_width="300dp"
android:layout_height="169dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:src="@drawable/bg_1" />
<Button
android:id="@+id/bSetWallpaper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="Set Image To Background" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/IVimage1"
android:layout_width="110dp"
android:layout_height="66dp"
android:padding="5dp"
android:src="@drawable/bg_1"/>
<ImageView
android:id="@+id/IVimage2"
android:layout_width="110dp"
android:layout_height="66dp"
android:padding="5dp"
android:src="@drawable/bg_2" />
<ImageView
android:id="@+id/IVimage3"
android:layout_width="110dp"
android:layout_height="66dp"
android:padding="5dp"
android:src="@drawable/bg_3" />
<ImageView
android:id="@+id/IVimage4"
android:layout_width="110dp"
android:layout_height="66dp"
android:padding="5dp"
android:src="@drawable/bg_4" />
<ImageView
android:id="@+id/IVimage5"
android:layout_width="110dp"
android:layout_height="66dp"
android:padding="5dp"
android:src="@drawable/bg_5" />
<ImageView
android:id="@+id/IVimage6"
android:layout_width="110dp"
android:layout_height="66dp"
android:padding="5dp"
android:src="@drawable/bg_6" />
<ImageView
android:id="@+id/IVimage7"
android:layout_width="110dp"
android:layout_height="66dp"
android:padding="5dp"
android:src="@drawable/bg_7" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
日志猫:
12-04 20:38:53.776 12776-12776/basics.noaml.com.basics V/Zygote﹕ Switching descriptor 59 to /dev/null
12-04 20:38:53.776 12776-12776/basics.noaml.com.basics V/Zygote﹕ Switching descriptor 10 to /dev/null
12-04 20:38:53.776 12776-12776/basics.noaml.com.basics D/dalvikvm﹕ Late-enabling CheckJNI
12-04 20:38:53.916 12776-12776/basics.noaml.com.basics D/ActivityThread﹕ handleBindApplication:basics.noaml.com.basics
12-04 20:38:53.916 12776-12776/basics.noaml.com.basics D/ActivityThread﹕ setTargetHeapUtilization:0.75
12-04 20:38:53.916 12776-12776/basics.noaml.com.basics D/ActivityThread﹕ setTargetHeapMinFree:524288
12-04 20:38:53.966 12776-12776/basics.noaml.com.basics I/dalvikvm﹕ DexOpt: illegal method access (call Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V from Landroid/content/res/XResources$XTypedArray;)
12-04 20:38:53.966 12776-12776/basics.noaml.com.basics I/dalvikvm﹕ Could not find method android.content.res.TypedArray.<init>, referenced from method android.content.res.XResources$XTypedArray.<init>
12-04 20:38:53.966 12776-12776/basics.noaml.com.basics W/dalvikvm﹕ VFY: unable to resolve direct method 82: Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V
12-04 20:38:53.966 12776-12776/basics.noaml.com.basics D/dalvikvm﹕ VFY: replacing opcode 0x70 at 0x0002
12-04 20:38:54.136 12776-12776/basics.noaml.com.basics E/MediaPlayer﹕ Should have subtitle controller already set
12-04 20:38:54.166 12776-12776/basics.noaml.com.basics I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
12-04 20:38:54.186 12776-12776/basics.noaml.com.basics D/OpenGLRenderer﹕ Enabling debug mode 0
12-04 20:38:54.206 12776-12776/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:38:54.266 12776-12776/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41de9ea8 time:1381137
12-04 20:38:55.176 12776-12776/basics.noaml.com.basics D/dalvikvm﹕ JIT code cache reset in 0 ms (4096 bytes 4/0)
12-04 20:38:55.356 12776-12776/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@4259c638 time:1382223
12-04 20:38:57.166 12776-12776/basics.noaml.com.basics E/ActivityThread﹕ Performing stop of activity that is not resumed: {basics.noaml.com.basics/basics.noaml.com.basics.menu}
java.lang.RuntimeException: Performing stop of activity that is not resumed: {basics.noaml.com.basics/basics.noaml.com.basics.menu}
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3164)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3251)
at android.app.ActivityThread.access$1100(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1232)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5139)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
12-04 20:39:28.176 12776-12776/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@4259c638 time:1415041
12-04 20:39:30.926 12776-12776/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:39:31.556 12776-12776/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:39:31.686 12776-12776/basics.noaml.com.basics E/MediaPlayer﹕ Should have subtitle controller already set
12-04 20:39:32.236 12776-12776/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41dfc710 time:1419108
12-04 20:39:32.976 12776-12776/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:39:33.546 12776-12776/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:39:33.576 12776-12776/basics.noaml.com.basics D/AndroidRuntime﹕ Shutting down VM
12-04 20:39:33.576 12776-12776/basics.noaml.com.basics W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415fbce0)
12-04 20:39:33.586 12776-12776/basics.noaml.com.basics E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: basics.noaml.com.basics, PID: 12776
java.lang.NullPointerException
at android.content.res.Resources.openRawResource(Resources.java:1085)
at android.content.res.Resources.openRawResource(Resources.java:1056)
at basics.noaml.com.basics.TutorialThree.onClick(TutorialThree.java:97)
at android.view.View.performClick(View.java:4445)
at android.view.View$PerformClick.run(View.java:18446)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5139)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
12-04 20:39:35.626 12776-12776/basics.noaml.com.basics I/Process﹕ Sending signal. PID: 12776 SIG: 9
12-04 20:39:35.806 13778-13778/basics.noaml.com.basics D/ActivityThread﹕ handleBindApplication:basics.noaml.com.basics
12-04 20:39:35.806 13778-13778/basics.noaml.com.basics D/ActivityThread﹕ setTargetHeapUtilization:0.75
12-04 20:39:35.806 13778-13778/basics.noaml.com.basics D/ActivityThread﹕ setTargetHeapMinFree:524288
12-04 20:39:35.816 13778-13778/basics.noaml.com.basics I/dalvikvm﹕ DexOpt: illegal method access (call Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V from Landroid/content/res/XResources$XTypedArray;)
12-04 20:39:35.816 13778-13778/basics.noaml.com.basics I/dalvikvm﹕ Could not find method android.content.res.TypedArray.<init>, referenced from method android.content.res.XResources$XTypedArray.<init>
12-04 20:39:35.816 13778-13778/basics.noaml.com.basics W/dalvikvm﹕ VFY: unable to resolve direct method 82: Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V
12-04 20:39:35.816 13778-13778/basics.noaml.com.basics D/dalvikvm﹕ VFY: replacing opcode 0x70 at 0x0002
12-04 20:39:35.916 13778-13778/basics.noaml.com.basics I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
12-04 20:39:35.926 13778-13778/basics.noaml.com.basics D/OpenGLRenderer﹕ Enabling debug mode 0
12-04 20:39:35.976 13778-13778/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41deb0e0 time:1422847
12-04 20:41:40.391 13778-13778/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41deb0e0 time:1547235
12-04 20:41:41.551 13778-13778/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:41:41.691 13778-13778/basics.noaml.com.basics E/MediaPlayer﹕ Should have subtitle controller already set
12-04 20:41:41.761 13778-13778/basics.noaml.com.basics D/dalvikvm﹕ JIT code cache reset in 0 ms (4096 bytes 4/0)
12-04 20:41:41.771 13778-13789/basics.noaml.com.basics W/MediaPlayer-JNI﹕ MediaPlayer finalized without being released
12-04 20:41:42.361 13778-13778/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42425358 time:1549209
12-04 20:41:43.001 13778-13778/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:41:43.471 13778-13778/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:41:43.711 13778-13778/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:41:44.641 13778-13778/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:41:45.091 13778-13778/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 20:41:45.181 13778-13778/basics.noaml.com.basics D/AndroidRuntime﹕ Shutting down VM
12-04 20:41:45.191 13778-13778/basics.noaml.com.basics W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415fbce0)
12-04 20:41:45.191 13778-13778/basics.noaml.com.basics E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: basics.noaml.com.basics, PID: 13778
java.lang.NullPointerException
at android.content.res.Resources.openRawResource(Resources.java:1085)
at android.content.res.Resources.openRawResource(Resources.java:1056)
at basics.noaml.com.basics.TutorialThree.onClick(TutorialThree.java:97)
at android.view.View.performClick(View.java:4445)
at android.view.View$PerformClick.run(View.java:18446)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5139)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
12-04 20:41:46.511 14049-14049/basics.noaml.com.basics D/ActivityThread﹕ handleBindApplication:basics.noaml.com.basics
12-04 20:41:46.511 14049-14049/basics.noaml.com.basics D/ActivityThread﹕ setTargetHeapUtilization:0.75
12-04 20:41:46.511 14049-14049/basics.noaml.com.basics D/ActivityThread﹕ setTargetHeapMinFree:524288
12-04 20:41:46.531 14049-14049/basics.noaml.com.basics I/dalvikvm﹕ DexOpt: illegal method access (call Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V from Landroid/content/res/XResources$XTypedArray;)
12-04 20:41:46.531 14049-14049/basics.noaml.com.basics I/dalvikvm﹕ Could not find method android.content.res.TypedArray.<init>, referenced from method android.content.res.XResources$XTypedArray.<init>
12-04 20:41:46.531 14049-14049/basics.noaml.com.basics W/dalvikvm﹕ VFY: unable to resolve direct method 82: Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V
12-04 20:41:46.531 14049-14049/basics.noaml.com.basics D/dalvikvm﹕ VFY: replacing opcode 0x70 at 0x0002
12-04 20:41:46.641 14049-14049/basics.noaml.com.basics I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
12-04 20:41:46.661 14049-14049/basics.noaml.com.basics D/OpenGLRenderer﹕ Enabling debug mode 0
12-04 20:41:46.711 14049-14049/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41de76f8 time:1553553
12-04 20:41:53.021 14049-14049/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41de76f8 time:1559863
12-04 21:15:03.174 18325-18325/basics.noaml.com.basics V/Zygote﹕ Switching descriptor 59 to /dev/null
12-04 21:15:03.174 18325-18325/basics.noaml.com.basics V/Zygote﹕ Switching descriptor 10 to /dev/null
12-04 21:15:03.184 18325-18325/basics.noaml.com.basics D/dalvikvm﹕ Late-enabling CheckJNI
12-04 21:15:03.304 18325-18325/basics.noaml.com.basics D/ActivityThread﹕ handleBindApplication:basics.noaml.com.basics
12-04 21:15:03.304 18325-18325/basics.noaml.com.basics D/ActivityThread﹕ setTargetHeapUtilization:0.75
12-04 21:15:03.304 18325-18325/basics.noaml.com.basics D/ActivityThread﹕ setTargetHeapMinFree:524288
12-04 21:15:03.324 18325-18325/basics.noaml.com.basics I/dalvikvm﹕ DexOpt: illegal method access (call Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V from Landroid/content/res/XResources$XTypedArray;)
12-04 21:15:03.324 18325-18325/basics.noaml.com.basics I/dalvikvm﹕ Could not find method android.content.res.TypedArray.<init>, referenced from method android.content.res.XResources$XTypedArray.<init>
12-04 21:15:03.324 18325-18325/basics.noaml.com.basics W/dalvikvm﹕ VFY: unable to resolve direct method 82: Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V
12-04 21:15:03.324 18325-18325/basics.noaml.com.basics D/dalvikvm﹕ VFY: replacing opcode 0x70 at 0x0002
12-04 21:15:03.474 18325-18325/basics.noaml.com.basics E/MediaPlayer﹕ Should have subtitle controller already set
12-04 21:15:03.504 18325-18325/basics.noaml.com.basics I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
12-04 21:15:03.534 18325-18325/basics.noaml.com.basics D/OpenGLRenderer﹕ Enabling debug mode 0
12-04 21:15:03.604 18325-18325/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41deaae0 time:2663065
12-04 21:15:04.534 18325-18325/basics.noaml.com.basics D/dalvikvm﹕ JIT code cache reset in 0 ms (4096 bytes 4/0)
12-04 21:15:04.714 18325-18325/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@4259b380 time:2664178
12-04 21:15:09.634 18325-18325/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 21:15:09.754 18325-18325/basics.noaml.com.basics E/MediaPlayer﹕ Should have subtitle controller already set
12-04 21:15:10.474 18325-18325/basics.noaml.com.basics I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41e5fe18 time:2669939
12-04 21:15:11.364 18325-18325/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 21:15:12.734 18325-18325/basics.noaml.com.basics I/Xposed﹕ xxxxxx
12-04 21:15:12.804 18325-18325/basics.noaml.com.basics D/AndroidRuntime﹕ Shutting down VM
12-04 21:15:12.804 18325-18325/basics.noaml.com.basics W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415fbce0)
12-04 21:15:12.804 18325-18325/basics.noaml.com.basics E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: basics.noaml.com.basics, PID: 18325
java.lang.NullPointerException
at android.content.res.Resources.openRawResource(Resources.java:1085)
at android.content.res.Resources.openRawResource(Resources.java:1056)
at basics.noaml.com.basics.TutorialThree.onClick(TutorialThree.java:97)
at android.view.View.performClick(View.java:4445)
at android.view.View$PerformClick.run(View.java:18446)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5139)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)