繁体   English   中英

如果情况导致程序强制关闭

[英]If condition causing program to force close

我正在做一个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;
        }
    }



}

这是我检查哪个变量设置为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);
        }

    }



    }

我可以在不使用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

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

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

您不能以这种方式访问​​其他活动变量,例如,更好的方法是使用常量类。

public class Constants {
    public static int water=0;
    public staticint fire=0;
}

主要活动:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch(v.getId()){
    case R.id.imageButton1:
        Constants.water = 1;
        Intent wi = new Intent("com.example.themetest.THEME");
        startActivity(wi);
        break;

    case R.id.imageButton2:
        Constants.fire = 1;
        Intent fi = new Intent("com.example.themetest.THEME");
        startActivity(fi);
        break;
    }
}

主题:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.theme);

    if(Constants.water==1){


        res = getResources(); 
        drawable = res.getDrawable(R.drawable.water_theme); 
        linearLayout =   (LinearLayout)findViewById(R.id.ll); 
        linearLayout.setBackgroundDrawable(drawable);
        }
        else if(Constants.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);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM