I’m using eclipse and making an android app
I’m trying to have a saved setting for a button but when I try to set the button text when the app starts up it fails and i get lots of errors.
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");
}
if I delete the button.setText("mls"); line it works fine
this is the logcat message
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
in the onCreate Method the line
setContentView(R.layout.main);loads only main.xml so the button hasn’t been loaded yet and the button text can’t be changed.but by the time
onCreateOptionsMenu(Menu menu)gets run the tab.xml files for the swiping view are loaded.