Android Studio SharedPreferences和内部存储不起作用

时间:2019-02-12 09:43:17

标签: java android sharedpreferences android-internal-storage

我正在构建一个反馈Android应用程序,我需要保存输入,但是共享的首选项会导致崩溃,并且内部存储无法正常工作

  // Shared Preferences Code // Crashes
     final String radioValues =
                    ((RadioButton)findViewById(rGrop.getCheckedRadioButtonId()))
                            .getText().toString();
           final String nameValue = Ename.getText().toString();
           final String cityValue = Ecity.getText().toString();
           final String phoneNum = Ephone.getText().toString();
           final String comments = ans.getText().toString();
           final float rbarValue = rBar.getNumStars();
            final float rbarValue2 = rBar2.getNumStars();
            final float rbarValue3 = rBar3.getNumStars();

            SharedPreferences settings = getApplicationContext().getSharedPreferences("Visitors Data",MODE_PRIVATE );
            SharedPreferences.Editor editor = settings.edit();

            editor.putString("Visitor Name: ",nameValue);
            editor.putString("Visitor City: ",cityValue);
            editor.putString("visitor Phone Number: ",phoneNum);
            editor.putString("Radio Group Choice: ",radioValues);
            editor.putString("Comments: ",comments );
            editor.putFloat("rating bar : ", rbarValue);
            editor.putFloat("rating bar 2: ", rbarValue2);
            editor.putFloat("rating bar 3: ", rbarValue3);
            // save changes in SharedPreferences
            editor.apply();

下一个是内部存储代码

try{
            FileOutputStream fileout=openFileOutput("EnginesTechDATA.txt", MODE_PRIVATE);
            OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);

            outputWriter.write(Ecity.getText().toString());
            outputWriter.write(Ename.getText().toString());
            outputWriter.write(Ephone.getText().toString());
outputWriter.write(ans.getText().toString());

outputWriter.write(rBar.getNumStars());
outputWriter.write(rBar2.getNumStars());
outputWriter.write(rBar3.getNumStars());

final String radioValues =
        ((RadioButton)findViewById(rGrop.getCheckedRadioButtonId()))
                .getText().toString();
outputWriter.write(radioValues);

outputWriter.close(); } catch (Exception e) {
e.printStackTrace(); }

我希望有人可以帮助我以任何方式保存这些数据 我明天需要它

这些是错误

  

02-12 12:58:25.170 10593-10593 /? E / CfgFilePolicy:****错误:未设置en CUST_POLICY_DIRS,请使用默认值   02-12 12:58:38.510 10593-10593 / com.example.etq E / AndroidRuntime:FATAL EXCEPTION:main       流程:com.example.etq,PID:10593       java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.CharSequence android.widget.RadioButton.getText()'           在com.example.etq.MainActivity $ 1.onClick(MainActivity.java:154)           在android.view.View.performClick(View.java:4792)           在android.view.View $ PerformClick.run(View.java:19938)           在android.os.Handler.handleCallback(Handler.java:739)           在android.os.Handler.dispatchMessage(Handler.java:95)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5601)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:964)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

XML广播组代码

<RadioGroup
        android:id="@+id/Rgrp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        >

        <RadioButton
            android:id="@+id/rBtn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="end"
            android:fontFamily="@font/lalezar"
            />

        <RadioButton
            android:id="@+id/rBtn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lalezar"
            />

        <RadioButton
            android:id="@+id/rBtn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="@font/lalezar"
            />
    </RadioGroup>

这是java文件中的行

        final RadioGroup rGrop = findViewById(R.id.Rgrp);

2 个答案:

答案 0 :(得分:1)

关于崩溃报告,这似乎是一个空指针异常。您试图在RadioButton对象上调用getText()方法,但是radioButton视图可能尚未初始化。例如,您可能忘记了调用 radioButton = findViewById(R.id.radio_button_id); (这是语法,请使用您的RadioButton变量名称和ID)

答案 1 :(得分:1)

根据您的崩溃日志

Java.lang.NullPointerException:尝试调用虚拟方法'java.lang.CharSequence android.widget.RadioButton.getText()'

您正试图从空单选按钮获取文本

您的单选按钮为空,这是导致崩溃的原因

检查您的布局文件和活动,确保单选按钮位于xml中,并且在活动中,您可以通过findViewById将其链接为xml中给出的相同ID

获得所有需要后,我建议您从要执行操作的地方调用此方法

public void saveCheckedBox()
{

//rGrop.getCheckedRadioButtonId() returns the id of the RadioButton(or -1 if no RadioButtons are checked) that is checked in the Radiogroup
if(rGrop.getCheckedRadioButtonId()==-1)
{
Toast.makeText(context, "All checkbox unchecked", Toast.LENGTH_SHORT).show();
return;
// if no radio button checked simply toast and return
}

     final String radioValues =
                    ((RadioButton)findViewById(rGrop.getCheckedRadioButtonId()))
                            .getText().toString();
           final String nameValue = Ename.getText().toString();
           final String cityValue = Ecity.getText().toString();
           final String phoneNum = Ephone.getText().toString();
           final String comments = ans.getText().toString();
           final float rbarValue = rBar.getNumStars();
            final float rbarValue2 = rBar2.getNumStars();
            final float rbarValue3 = rBar3.getNumStars();

            SharedPreferences settings = getApplicationContext().getSharedPreferences("Visitors Data",MODE_PRIVATE );
            SharedPreferences.Editor editor = settings.edit();

            editor.putString("Visitor Name: ",nameValue);
            editor.putString("Visitor City: ",cityValue);
            editor.putString("visitor Phone Number: ",phoneNum);
            editor.putString("Radio Group Choice: ",radioValues);
            editor.putString("Comments: ",comments );
            editor.putFloat("rating bar : ", rbarValue);
            editor.putFloat("rating bar 2: ", rbarValue2);
            editor.putFloat("rating bar 3: ", rbarValue3);
            // save changes in SharedPreferences
            editor.apply();


}