【问题标题】:Android Begineer: java.lang.NullPointerExceptionAndroid 初学者:java.lang.NullPointerException
【发布时间】:2012-05-15 22:28:31
【问题描述】:

我是 android/java 新手,所以请多多包涵。我的代码之前运行良好,但自从我添加了 for() 循环后,我就收到了 NullPointerException。有什么想法吗?

public class PreferencesActivity extends Activity {

SharedPreferences settings;
SharedPreferences.Editor editor;
static CheckBox box, box2;

private final static CheckBox[] boxes={box, box2};
private final static String[] checkValues={"checked1", "checked2"};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    box=(CheckBox)findViewById(R.id.checkBox);
    box2=(CheckBox)findViewById(R.id.checkBox2);

    settings= getSharedPreferences("MyBoxes", 0);
    editor = settings.edit();

    if(settings != null){

    for(int i =0;i<boxes.length; i++)
        boxes[i].setChecked(settings.getBoolean(checkValues[i], false));   
    }

}

@Override
protected void onStop(){
   super.onStop();

   for(int i = 0; i <boxes.length; i++){           
   boolean checkBoxValue = boxes[i].isChecked();        
   editor.putBoolean(checkValues[i], checkBoxValue);
   editor.commit();       
   }

    }
}

【问题讨论】:

  • 请提供 logcat 跟踪信息,我认为您的 for 语句中的框为空

标签: java android eclipse android-layout android-intent


【解决方案1】:

您将box 和box2 的值初始化为null(因为这是未显式分配时的默认值)。然后在创建 Checkbox 数组 boxes 时使用这些值。因此,boxes 有两个空值。然后重新分配box 和box2 的值。请注意,这对boxes 数组中的值没有影响。因此,当您尝试访问数组中的值时,您会得到一个NullPointerException。

在您为box 和box2 分配值之后在boxes 内设置值。

【讨论】:

  • 你能举个例子说明我会怎么做吗?
【解决方案2】:

这是固定代码。希望对您有所帮助:

public class PreferencesActivity extends Activity {

    SharedPreferences settings;
    SharedPreferences.Editor editor;
    static CheckBox box, box2; // box and box2 are null when class is loaded to DalvikVM

    private final static CheckBox[] boxes={null, null};
    private final static String[] checkValues={"checked1", "checked2"};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        box=(CheckBox)findViewById(R.id.checkBox);
        box2=(CheckBox)findViewById(R.id.checkBox2);

        // assign CheckBox instances to boxes array
        boxes[0] = box;
        boxes[1] = box2;

        settings= getSharedPreferences("MyBoxes", 0);
        editor = settings.edit();

        if(settings != null){

        for(int i =0;i<boxes.length; i++)
            boxes[i].setChecked(settings.getBoolean(checkValues[i], false));   
        }

    }

    @Override
    protected void onStop() {
        super.onStop();

        for(int i = 0; i <boxes.length; i++){           
            boolean checkBoxValue = boxes[i].isChecked();        
            editor.putBoolean(checkValues[i], checkBoxValue);
            editor.commit();       
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多