Java 当我点击generate1按钮时,它崩溃了。共享引用相关问题

Java 当我点击generate1按钮时,它崩溃了。共享引用相关问题,java,android,Java,Android,我想做的是: 1.检查Num2值是否存在,如果未设置为零 2.随机生成num1中的新数字0~100 3.如果num1为偶数,则将num2增加1 4.保存值并重复 当我点击按钮时,它崩溃了。原因可能是什么 日志: import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.w

我想做的是:

1.检查Num2值是否存在,如果未设置为零

2.随机生成num1中的新数字0~100

3.如果num1为偶数,则将num2增加1

4.保存值并重复

当我点击按钮时,它崩溃了。原因可能是什么

日志:

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);
    }
}

在使用代码之前,将mySharedPreferences=PreferenceManeger.GetDefaultSharedReferences(this)添加到代码中。(假设您希望使用默认首选项。)

在使用代码之前,将
mySharedPreferences=PreferenceManeger.getDefaultSharedPreferences(此)
添加到代码中。(假设您想使用默认首选项。)

mySharedPreferences
null
probablyPost(使用日志添加到您的问题)。我添加了日志记录。我该怎么办?如果正确答案对你有帮助,你可以投票选出正确答案。
mySharedPreferences
is
null
probablyPost(使用日志添加到问题中)。我添加了日志记录。我该怎么办?如果对你有帮助,你可以投票选出正确的答案。
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)