启动Android轻扫视图应用时无法更改按钮文本

时间:2013-01-21 04:08:49

标签: android views swipe

我正在使用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

1 个答案:

答案 0 :(得分:0)

在onCreate方法中,行setContentView(R.layout.main);仅加载main.xml,因此尚未加载按钮,并且无法更改按钮文本。  但是当onCreateOptionsMenu(Menu menu)运行时,将加载滑动视图的tab.xml文件。