Android 使用SharedReferences时出现运行时异常

Android 使用SharedReferences时出现运行时异常,android,sharedpreferences,android-alertdialog,Android,Sharedpreferences,Android Alertdialog,免责声明:我以前从未使用过SharedReferences,而且我第一次尝试也不太顺利 我想为应用程序保存一个常量值。我认为SharedReferences是一个不错的选择,因为我只想向用户请求一次这个值,而这是用户第一次打开应用程序的时候 这是我的密码 SharedPreferences runCheck = getApplicationContext().getSharedPreferences("hasRunBefore", 0); //load the preferences B

免责声明:我以前从未使用过SharedReferences,而且我第一次尝试也不太顺利

我想为应用程序保存一个常量值。我认为SharedReferences是一个不错的选择,因为我只想向用户请求一次这个值,而这是用户第一次打开应用程序的时候

这是我的密码

SharedPreferences runCheck = getApplicationContext().getSharedPreferences("hasRunBefore", 0); //load the preferences
    Boolean hasRun = runCheck.getBoolean("hasRun", false); //see if it's run before, default no
    if (!hasRun) {
//This code runs the first time the app is opened
        SharedPreferences settings = getSharedPreferences("hasRunBefore", 0);
        SharedPreferences.Editor edit = settings.edit();
        edit.putBoolean("hasRun", true); //set to has run

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.alertdialog,null))
        .setPositiveButton(AlertDialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EditText editText = (EditText)findViewById(R.id.alert_dialog_editText);
                if(editText.getText().toString() != null) {
                    regNum = editText.getText().toString();
                }
            }
        });
        edit.putString("registerNumber",regNum);
        builder.show();
        TextView textView = (TextView)findViewById(R.id.regNum_textView);
        textView.setText(regNum);
        edit.commit(); //apply
    }
    else
    {
        //code if the app HAS run before
        TextView textView = (TextView)findViewById(R.id.regNum_textView);
        textView.setText(regNum);
    }
以及为AlertDialog而膨胀的XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/alertDialog"
>

<TextView
    android:id="@+id/alert_dialog_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter Your Register Number"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/alert_dialog_editText"
    android:layout_below="@+id/alert_dialog_textview"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

我能看到的至少一个错误是,您在
onClick
中查找
EditText
,但您查找的是活动内部,而不是对话框的视图内部。因此,
editText
equalnull。此外,
SharedReferences
不需要有两个变量

    SharedPreferences sharedPreferences = getSharedPreferences("hasRunBefore", 0);
    boolean hasRun = sharedPreferences.getBoolean("hasRun", false);
    if (!hasRun) {
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean("hasRun", true);

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        final View dialogView = getLayoutInflater().inflate(R.layout.alertdialog, null);
        builder.setView(dialogView)
                .setPositiveButton(AlertDialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Here important change, before you had NPE
                        EditText editText = (EditText) dialogView.findViewById(R.id.alert_dialog_editText);
                        if (editText.getText().toString() != null) {
                            regNum = editText.getText().toString();
                        }
                    }
                });
        edit.putString("registerNumber", regNum);
        builder.show();
        TextView textView = (TextView) findViewById(R.id.regNum_textView);
        textView.setText(regNum);
        edit.commit(); //apply
    } else {
        //code if the app HAS run before
        TextView textView = (TextView) findViewById(R.id.regNum_textView);
        textView.setText(regNum);
    }
更新:

setPositiveButton
,方法专家将字符串资源的id作为第一个参数,以便将其显示为标签,但您正在传递
AlertDialog.BUTTON\u positiveButton
,首先它没有任何意义,其次它会使您的应用程序崩溃

因此,您可以在字符串资源上传递id,也可以只传递字符串,例如:


setPositiveButton(“标签”…

请显示logcat中的堆栈跟踪。您好,我想,但logcat似乎没有停止!它一直充斥着新文本。单击它并滚动到堆栈跟踪位置。将有一个下拉图标..可在AndroidMonitor中选择..因此可以向下拖动它或键入“System.error”…在logCate的搜索字段中(ctrl+f)因此,它确实会显示这些行。我删除了额外的变量,并将视图引用到对话框中的一个。我已使用logcat更新了我的答案!如果我用字符串替换AlertDialog.BUTTON_正数,代码将运行。没有空指针异常。但是,我无法在EditText字段中键入内容。光标为blinking,但我无法在该字段中键入任何内容。是的,您的代码中可能还有其他错误。如果您尝试逐个本地化这些错误,并在仍然需要时创建新问题。该问题已得到回答。
    SharedPreferences sharedPreferences = getSharedPreferences("hasRunBefore", 0);
    boolean hasRun = sharedPreferences.getBoolean("hasRun", false);
    if (!hasRun) {
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean("hasRun", true);

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        final View dialogView = getLayoutInflater().inflate(R.layout.alertdialog, null);
        builder.setView(dialogView)
                .setPositiveButton(AlertDialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Here important change, before you had NPE
                        EditText editText = (EditText) dialogView.findViewById(R.id.alert_dialog_editText);
                        if (editText.getText().toString() != null) {
                            regNum = editText.getText().toString();
                        }
                    }
                });
        edit.putString("registerNumber", regNum);
        builder.show();
        TextView textView = (TextView) findViewById(R.id.regNum_textView);
        textView.setText(regNum);
        edit.commit(); //apply
    } else {
        //code if the app HAS run before
        TextView textView = (TextView) findViewById(R.id.regNum_textView);
        textView.setText(regNum);
    }