简体   繁体   中英

Android Studio SharedPreferences and Internal Storage not working

I am building a feedback Android application, and i need to save the inputs but the shared preferences is causing a crash, and the internal storage is not working

  // 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();

and the next one is the Internal Storage Code

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

i hope someone can help i need to save these data by any way and i need it by tomorrow

These are the Errors

02-12 12:58:25.170 10593-10593/? E/CfgFilePolicy: ****ERROR: env CUST_POLICY_DIRS not set, use default 02-12 12:58:38.510 10593-10593/com.example.etq E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.etq, PID: 10593 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference at com.example.etq.MainActivity$1.onClick(MainActivity.java:154) at android.view.View.performClick(View.java:4792) at android.view.View$PerformClick.run(View.java:19938) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5601) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

XML Radio Group code

<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>

and this is the line in java file

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

Regarding your crash report, it seems like a Null Pointer Exception. You tried to call getText() method on a RadioButton object, but the radioButton view probably wasn't initialized. for example, you might have forgot to call radioButton = findViewById(R.id.radio_button_id); (this is syntax, use your RadioButton variable name and id)

As per as your crash log

Java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()'

You are trying to get text from null radio button

Your radio button is null and that is the cause of crash

Check your layout file and activity , make sure radio button is in xml and in activity you link it via findViewById with same id given in xml

After getting all your need i suggest you to call this method from where you want to perform action

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


} 

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