重新获得按钮可见性时遇到问题

时间:2015-08-15 06:33:29

标签: java android button visibility

我有几个按钮,我设置为可见并按下按钮,但当我尝试调用onResume中的按钮状态时,它正在崩溃。我在代码中做错了什么?任何帮助将不胜感激。

public class MainActivity extends AppCompatActivity {

Button add; Button del; Button button1; Button button2;

private Boolean set1; private Boolean set2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    add = (Button)findViewById(R.id.add);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showButton();
        }
    });

    del = (Button)findViewById(R.id.del);
    del.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            delBtn();
        }
    });
    button1 = (Button)findViewById(R.id.button1);
    button2 = (Button)findViewById(R.id.button2);

}

public void showButton(){
    Boolean btn1 = button1.isShown();

    if(!button1.isShown()){
        button1.setVisibility(View.VISIBLE);
    }
    if(btn1 && !button2.isShown()){
        button2.setVisibility(View.VISIBLE);
    }

}

public void delBtn(){
    Boolean bt1 = !button2.isShown();
    if(button2.isShown()){
        button2.setVisibility(View.GONE);
    }

    if (bt1 && button1.isShown()){
        button1.setVisibility(View.GONE);
    }
}

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

    SharedPreferences.Editor editor = getPreferences(0).edit();
    set1 = button1.isShown();
    set2 = button2.isShown();

    editor.putBoolean("set1", set1);
    editor.putBoolean("set2", set2);

    editor.apply();

}

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

    SharedPreferences prefs = getPreferences(0);
    Boolean restoreSet1 = prefs.getBoolean("set1", set1); //in the Logcat, this is where it says the error is. Am i making the wrong call to restore button state?
    if(restoreSet1){
        button1.setVisibility(View.VISIBLE);
    }
    Boolean restoreSet2 = prefs.getBoolean("set2", set2);
    if(restoreSet2){
        button2.setVisibility(View.VISIBLE);
    }

 }
}

这是我的logcat:

  Caused by: java.lang.NullPointerException
        at lp.buttonApp.MainActivity.onResume(MainActivity.java:83)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1210)
        at android.app.Activity.performResume(Activity.java:5505)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2936)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2975)
        android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
        at android.app.ActivityThread.access$900(ActivityThread.java:161)

android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)

        com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我认为您可能遇到共享偏好问题。

请使用以下内容保存到共享首选项中:

<?php 
 $user_input = NULL;

 if (isset ($_POST['user_input'])&& !empty($_POST['user_input']))
 {
    echo $user_input = $_POST['user_input'];
 }

echo $user_input; 
?>

<br>
<hr>
<form action="51consoringWords.php" method="POST">
    <textarea name = "user_input" rows="6" cols="30"></textarea><br>
    <input type="submit" value="Submit">
</form>

为了阅读首选项:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

您可以参考this链接