Note that there are some explanatory texts on larger screens.

plurals
  1. POIf condition causing program to force close
    primarykey
    data
    text
    <p>I am doing an android app and i was thinking of creating a theme option. Well all I was thinking of doing was to allow the user to click on a image button which represented a theme. And when he clicks on it I start a new activity i.e i direct him to the home page. Also i have created a few integer variables which are set to 1 when the user clicks on a button. </p> <p>Then in the other classes all i do is check if the variables are 1 or not and depending on that i apply the theme. By theme i mean i just change the background wallpaper. But this is not working. I mean the code works but if use an if loop to check the variable values and then apply the effects it causes an error.</p> <p>Here's the complete code:</p> <pre><code>package com.example.themetest; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton; import android.widget.LinearLayout; public class MainActivity extends Activity implements OnClickListener{ ImageButton ib1; ImageButton ib2; int water=0; int fire=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ib1 = (ImageButton) findViewById(R.id.imageButton1); ib2 = (ImageButton) findViewById(R.id.imageButton2); ib1.setOnClickListener(this); ib2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.imageButton1: water = 1; Intent wi = new Intent("com.example.themetest.THEME"); startActivity(wi); break; case R.id.imageButton2: fire = 1; Intent fi = new Intent("com.example.themetest.THEME"); startActivity(fi); break; } } } </code></pre> <p>Here's the other class where i check which variable is set to 1 and apply the effect.</p> <pre><code>package com.example.themetest; import java.io.InputStream; import android.app.Activity; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.EditText; import android.widget.LinearLayout; public class Theme extends Activity{ MainActivity main; Resources res; Drawable drawable; LinearLayout linearLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.theme); if(main.water==1){ res = getResources(); drawable = res.getDrawable(R.drawable.water_theme); linearLayout = (LinearLayout)findViewById(R.id.ll); linearLayout.setBackgroundDrawable(drawable); } else if(main.fire==1){ res = getResources(); drawable = res.getDrawable(R.drawable.fire_theme); linearLayout = (LinearLayout)findViewById(R.id.ll); linearLayout.setBackgroundDrawable(drawable); } else{ res = getResources(); drawable = res.getDrawable(R.drawable.ic_launcher); linearLayout = (LinearLayout)findViewById(R.id.ll); linearLayout.setBackgroundDrawable(drawable); } } } </code></pre> <p>I can change the wallpaper without using the if loop, but i want to do it this way which is not working. Can anyone please tell me why?</p> <p>Log cat:</p> <pre><code>01-15 12:08:23.339: D/dalvikvm(273): GC_EXTERNAL_ALLOC freed 767 objects / 55936 bytes in 235ms 01-15 12:08:25.539: D/AndroidRuntime(273): Shutting down VM 01-15 12:08:25.539: W/dalvikvm(273): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 01-15 12:08:25.559: E/AndroidRuntime(273): FATAL EXCEPTION: main 01-15 12:08:25.559: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.themetest/com.example.themetest.Theme}: java.lang.NullPointerException 01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 01-15 12:08:25.559: E/AndroidRuntime(273): at android.os.Handler.dispatchMessage(Handler.java:99) 01-15 12:08:25.559: E/AndroidRuntime(273): at android.os.Looper.loop(Looper.java:123) 01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-15 12:08:25.559: E/AndroidRuntime(273): at java.lang.reflect.Method.invokeNative(Native Method) 01-15 12:08:25.559: E/AndroidRuntime(273): at java.lang.reflect.Method.invoke(Method.java:521) 01-15 12:08:25.559: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-15 12:08:25.559: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-15 12:08:25.559: E/AndroidRuntime(273): at dalvik.system.NativeStart.main(Native Method) 01-15 12:08:25.559: E/AndroidRuntime(273): Caused by: java.lang.NullPointerException 01-15 12:08:25.559: E/AndroidRuntime(273): at com.example.themetest.Theme.onCreate(Theme.java:29) 01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-15 12:08:25.559: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 01-15 12:08:25.559: E/AndroidRuntime(273): ... 11 more 01-15 12:08:31.089: I/Process(273): Sending signal. PID: 273 SIG: 9 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload