简体   繁体   中英

When I clicked the generate1 button, It crashes. SharedPreferences Related Issue

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;


public class MainActivity extends AppCompatActivity {
    SharedPreferences mySharedPreferences;
    TextView t1,t2;
    Button generate1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        t1 = (TextView) findViewById(R.id.textView3);
        t2 = (TextView) findViewById(R.id.textView8);
        generate1 = (Button) findViewById(R.id.button);
        generate1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random rand = new Random();
                int num1 = rand.nextInt(100);
                int num2 = mySharedPreferences.getInt("INT_KEY", -1);
                if(num2 == -1) {
                    num2 = 0; //initialize the total count value to 0
                }
                    if (num1 % 2 == 0) {
                        num2 += 1;
                        SharedPreferences.Editor editor = mySharedPreferences.edit();
                        editor.putInt("INT_KEY", num2);
                        editor.commit();
                        t1.setText("" + num2);
                    }
                }
            });
        }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

What I want to do is :

1.Check Whether Num2 value is exist or not, If not set to zero.

2.Randomly Generate new numbers 0~100 in num1

3.If num1 is Even, Increase num2 by 1.

4.Save Value and Repeat.

When I clicked the button, It crashes. What could be the reason?

Logcat:

10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 D/AndroidRuntime: Shutting down VM
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x423dcc08)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: FATAL EXCEPTION: main
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: Process: com.example.tans.goldminer1, PID: 30860
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime: java.lang.NullPointerException
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at com.example.tans.goldminer1.MainActivity$1.onClick(MainActivity.java:35)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at android.view.View.performClick(View.java:4753)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19567)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:146)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5635)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
10-30 13:39:36.418 30860-30860/com.example.tans.goldminer1 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

Add mySharedPreferences = PreferenceManeger.getDefaultSharedPreferences(this) to your code before you use it. (As assumed that you want to use default preferences.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM