簡體   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