我是Android应用程序开发的新手,我通过练习一些示例来学习它,并且尝试基于这些示例实现我自己的想法...作为我试图保存背景颜色的一部分屏幕使用Bundles和sharedpreferences。
此代码仅保存一次颜色,但是当我再次旋转屏幕时,应用程序正在崩溃
package com.radiobuttonproject;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnCheckedChangeListener,
OnClickListener {
// Initialisation area ....
RadioGroup myRadiogroup;
Button Apply;
TextView message;
RelativeLayout BackGround;
int CheckedId = 0;
SharedPreferences myPrefs;
Editor myEditor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRadiogroup = (RadioGroup) findViewById(R.id.myRadioGroup);
Apply = (Button) findViewById(R.id.Apply);
message = (TextView) findViewById(R.id.TextView);
BackGround = (RelativeLayout) findViewById(R.id.Background);
Apply.setOnClickListener(this);
// myRadiogroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
// Tasks to be performed on click of the radiobutton
switch (checkedId) {
case R.id.RadioButton1:
BackGround.setBackgroundColor(Color.RED);
// Saving the color using the sharedpreferences....
myPrefs = getPreferences(Context.MODE_PRIVATE);
myEditor = myPrefs.edit();
myEditor.putInt("CHECKEDBUTTON", R.id.RadioButton1);
myEditor.commit();
break;
case R.id.RadioButton2:
BackGround.setBackgroundColor(Color.GREEN);
// Saving the color using the sharedpreferences....
myPrefs = getPreferences(Context.MODE_PRIVATE);
myEditor = myPrefs.edit();
myEditor.putInt("CHECKEDBUTTON", R.id.RadioButton2);
myEditor.commit();
break;
case R.id.RadioButton3:
BackGround.setBackgroundColor(Color.YELLOW);
// Saving the color using the sharedpreferences....
myPrefs = getPreferences(Context.MODE_PRIVATE);
myEditor = myPrefs.edit();
myEditor.putInt("CHECKEDBUTTON", R.id.RadioButton3);
myEditor.commit();
break;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Setting the background color using the id from RadioButton
CheckedId = myRadiogroup.getCheckedRadioButtonId();
onCheckedChanged(myRadiogroup, CheckedId);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
// Retrieving and Saving the color using the sharedpreferences and Bundle....
CheckedId = myPrefs.getInt("CHECKEDBUTTON", 0);
outState.putInt("CHECKEDBUTTON", CheckedId);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
int getCheckedId = savedInstanceState.getInt("CHECKEDBUTTON");
// Retrieving the color using the Bundle and setting it back to the background....
switch (getCheckedId) {
case R.id.RadioButton1:
BackGround.setBackgroundColor(Color.RED);
break;
case R.id.RadioButton2:
BackGround.setBackgroundColor(Color.GREEN);
break;
case R.id.RadioButton3:
BackGround.setBackgroundColor(Color.YELLOW);
}
}
}
02 - 22 21: 17: 35.468: I / Adreno - EGL(5727): < qeglDrvAPI_eglInitialize: 410 > : EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8226_LNX.LA.3.5.1_RB1__release_AU()
02 - 22 21: 17: 35.468: I / Adreno - EGL(5727): OpenGL ES Shader Compiler Version: E031.24.00.08
02 - 22 21: 17: 35.468: I / Adreno - EGL(5727): Build Date: 03 / 07 / 14 Fri
02 - 22 21: 17: 35.468: I / Adreno - EGL(5727): Local Branch:
02 - 22 21: 17: 35.468: I / Adreno - EGL(5727): Remote Branch: quic / LNX.LA.3.5.1_RB1.1
02 - 22 21: 17: 35.468: I / Adreno - EGL(5727): Local Patches: NONE
02 - 22 21: 17: 35.468: I / Adreno - EGL(5727): Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 + NOTHING
02 - 22 21: 17: 35.540: D / OpenGLRenderer(5727): Enabling debug mode 0
02 - 22 21: 18: 14.755: D / AndroidRuntime(5727): Shutting down VM
02 - 22 21: 18: 14.755: W / dalvikvm(5727): threadid = 1: thread exiting with uncaught exception(group = 0x41667d40)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): FATAL EXCEPTION: main
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): Process: com.radiobuttonproject, PID: 5727
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): java.lang.NullPointerException
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at com.radiobuttonproject.MainActivity.onSaveInstanceState(MainActivity.java: 86)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at android.app.Activity.performSaveInstanceState(Activity.java: 1161)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java: 1246)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java: 3779)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at android.app.ActivityThread.access$900(ActivityThread.java: 139)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1216)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at android.os.Handler.dispatchMessage(Handler.java: 102)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at android.os.Looper.loop(Looper.java: 136)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at android.app.ActivityThread.main(ActivityThread.java: 5086)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at java.lang.reflect.Method.invokeNative(Native Method)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at java.lang.reflect.Method.invoke(Method.java: 515)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 785)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 601)
02 - 22 21: 18: 14.831: E / AndroidRuntime(5727): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
只有在点击某些RadioButtons时才启动myPrefs
。如果您不这样做并旋转应用,则会尝试将颜色保存到未初始化的首选项,并获得NullPointerException
。