我正在使用 eclipse 并制作一个 android 应用程序
我正在尝试保存按钮的设置,但是当我尝试在应用程序启动时设置按钮文本时,它会失败并且出现很多错误。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
LoadPreferences();
.........
}private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
floz = sharedPreferences.getBoolean("floz", false);
Button button = (Button) findViewById(R.id.buttonVolumeType);
button.setText("mls");
}
如果我删除该button.setText("mls");
行它工作正常
这是 logcat 消息
12-22 23:35:38.064: E/Trace(1384): error opening trace file: No such file or directory (2) 12-22 23:35:38.663: D/AndroidRuntime(1384): Shutting down VM
12-22 23:35:38.663: W/dalvikvm(1384): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-22 23:35:38.684: E/AndroidRuntime(1384): FATAL EXCEPTION: main
12-22 23:35:38.684: E/AndroidRuntime(1384): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cheapchug/com.example.cheapchug.MainActivity}: java.lang.NullPointerException
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.os.Looper.loop(Looper.java:137)
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-22 23:35:38.684: E/AndroidRuntime(1384): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 23:35:38.684: E/AndroidRuntime(1384): at java.lang.reflect.Method.invoke(Method.java:511)
12-22 23:35:38.684: E/AndroidRuntime(1384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-22 23:35:38.684: E/AndroidRuntime(1384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-22 23:35:38.684: E/AndroidRuntime(1384): at dalvik.system.NativeStart.main(Native Method)
12-22 23:35:38.684: E/AndroidRuntime(1384): Caused by: java.lang.NullPointerException
12-22 23:35:38.684: E/AndroidRuntime(1384): at com.example.cheapchug.MainActivity.LoadPreferences(MainActivity.java:260)
12-22 23:35:38.684: E/AndroidRuntime(1384): at com.example.cheapchug.MainActivity.onCreate(MainActivity.java:70)
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.app.Activity.performCreate(Activity.java:5008)
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-22 23:35:38.684: E/AndroidRuntime(1384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-22 23:35:38.684: E/AndroidRuntime(1384): ... 11 more