如果条件导致程序强制关闭

mae*_*n11 1 java android if-statement

我正在做一个Android应用程序,我正在考虑创建一个主题选项.我所想做的就是让用户点击代表主题的图像按钮.当他点击它时,我开始一个新的活动,即我将他引导到主页.此外,我创建了一些整数变量,当用户单击按钮时,这些变量设置为1.

然后在其他类中我所做的就是检查变量是否为1,并根据我应用主题.按主题我的意思是我只是改变背景壁纸.但这不起作用.我的意思是代码工作,但如果使用if循环来检查变量值,然后应用效果,它会导致错误.

这是完整的代码:

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;
        }
    }



}
Run Code Online (Sandbox Code Playgroud)

这是另一个类,我检查哪个变量设置为1并应用效果.

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);
        }

    }



    }
Run Code Online (Sandbox Code Playgroud)

我可以在不使用if循环的情况下更改壁纸,但我想这样做,这是不行的.谁能告诉我为什么?

原木猫:

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
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 5

我认为更好的方法是传递所选主题用户的名称或代码以及意图.

这可能会有所帮助:通过Intent传递数据并接收它