【问题标题】:If condition causing program to force close如果条件导致程序强制关闭
【发布时间】:2013-01-15 15:21:35
【问题描述】:

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

【问题讨论】:

  • 错误是什么样的?可能是NullPointerEception,因为main 未初始化?
  • 我在任何地方都看不到main的初始化。
  • 这不是 android 的 Activity 类字段的工作方式。如果您想将一个 Activity 的值传递给下一个,请使用带有键值对的 Bundle 或 Intent。看看this post
  • 对不起。我是编程新手。但是我应该将 main 初始化为什么?

标签: java android if-statement


【解决方案1】:

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

这可能会有所帮助:Passing data through Intent and receiving it

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 2014-06-08
      相关资源
      最近更新 更多