[Q] How to set as wallpaper the current image displayed in the ImageView ?

Hello guys
I am trying to build an application with 2 activities one is the main activity and the second activity which contain ImageView, and a 3 button’s. I want when I press the button to set as wallpaper the current image displayed in the ImageView . The problem is when I set the code for the Set Wallpaper button the first main activity is accessed without problems, but when I try to access the second activity the app crashes as you can see in the log. If I don’t configure the Set Wallpaper button, the code for the next and previous buttons runs smoothly. I tried several options and methods to configure the onClick for the Set Wallpaper button. Please help me with this problem. Here is my code:


public class Castles extends Activity { protected static InputStream is;

public int currentImage = 0; int[] imageIds = { R.drawable.cas1,R.drawable.cas2,R.drawable.cas3,R.drawable.cas4,R.drawable.cas5,R.drawable.cas6, R.drawable.cas7, R.drawable.cas8, R.drawable.cas9,R.drawable.cas10,R.drawable.cas11,R.drawable.cas12, R.drawable.cas13,R.drawable.cas14,R.drawable.cas15,R.drawable.cas16, R.drawable.cas17,R.drawable.cas18, R.drawable.cas19,R.drawable.cas20,R.drawable.cas21}; private Button BNatNext; private Button BNatPri; private Button BSetWallNat; private ImageView IVNat; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.castles); IVNat = (ImageView)findViewById(R.id.IVNat); BNatNext=(Button)findViewById(R.id.BNatNext); BNatPri=(Button)findViewById(R.id.BNatPri); BNatNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub currentImage++; currentImage=currentImage%imageIds.length; IVNat.setImageResource(imageIds[currentImage]); } }); BNatPri.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { currentImage--; currentImage=(currentImage+imageIds.length)%imageIds.length; IVNat.setImageResource(imageIds[currentImage]); } }); BSetWallNat.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try {

} catch(IOException e){ Log.e("Tag", "couldn't set wallpaper", e);

} }

And this is the LogCat:

12-04 21:44:15.205: D/dalvikvm(1223): GC_EXTERNAL_ALLOC freed 44K, 51% free 2670K/5379K, external 1527K/1559K, paused 418ms 12-04 21:44:18.282: D/dalvikvm(1223): GC_EXTERNAL_ALLOC freed 1K, 51% free 2669K/5379K, external 4560K/5695K, paused 486ms 12-04 21:44:24.043: D/gralloc_goldfish(1223): Emulator without GPU emulation detected. 12-04 21:44:27.853: D/dalvikvm(1223): GC_EXTERNAL_ALLOC freed 11K, 50% free 2698K/5379K, external 7974K/7974K, paused 201ms 12-04 21:44:32.223: D/dalvikvm(1223): GC_EXTERNAL_ALLOC freed 2K, 50% free 2702K/5379K, external 11198K/11388K, paused 321ms 12-04 21:44:34.903: D/AndroidRuntime(1223): Shutting down VM 12-04 21:44:34.903: W/dalvikvm(1223): threadid=1: thread exiting with uncaught exception (group=0xb609d4f0) 12-04 21:44:34.963: E/AndroidRuntime(1223): FATAL EXCEPTION: main 12-04 21:44:34.963: E/AndroidRuntime(1223): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Roapps.wallpaperhd/com.Roapps.wallpaperhd.Castles}: java.lang.NullPointerException 12-04 21:44:34.963: E/AndroidRuntime(1223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 12-04 21:44:34.963: E/AndroidRuntime(1223): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 12-04 21:44:34.963: E/AndroidRuntime(1223): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 12-04 21:44:34.963: E/AndroidRuntime(1223): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 12-04 21:44:34.963: E/AndroidRuntime(1223): at android.os.Handler.dispatchMessage(Handler.java:99) 12-04 21:44:34.963: E/AndroidRuntime(1223): at android.os.Looper.loop(Looper.java:130) 12-04 21:44:34.963: E/AndroidRuntime(1223): at android.app.ActivityThread.main(ActivityThread.java:3683) 12-04 21:44:34.963: E/AndroidRuntime(1223): at java.lang.reflect.Method.invokeNative(Native Method) 12-04 21:44:34.963: E/AndroidRuntime(1223): at java.lang.reflect.Method.invoke(Method.java:507) 12-04 21:44:34.963: E/AndroidRuntime(1223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 12-04 21:44:34.963: E/AndroidRuntime(1223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 12-04 21:44:34.963: E/AndroidRuntime(1223): at dalvik.system.NativeStart.main(Native Method) 12-04 21:44:34.963: E/AndroidRuntime(1223): Caused by: java.lang.NullPointerException 12-04 21:44:34.963: E/AndroidRuntime(1223): at com.Roapps.wallpaperhd.Castles.onCreate(Castles.java:63) 12-04 21:44:34.963: E/AndroidRuntime(1223): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 12-04 21:44:34.963: E/AndroidRuntime(1223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 12-04 21:44:34.963: E/AndroidRuntime(1223): ... 11 more 12-04 21:44:38.963: I/Process(1223): Sending signal. PID: 1223 SIG: 9

Show Accepted Answer

Guest Quick Reply (No URL, BBcode or HTML)

Last post by frieda47
1 hour ago
Last post by wnolan
4 hours ago
Started by ncollins
Last post by erdman.isabella
1 hour ago
Last post by van96
3 hours ago
Last post by leuschke.luz
4 hours ago