简体   繁体   中英

How do I get the value of a Spinner of another class? (Getting NullPointerException)

I have two classes Settings and MainActivity . I am trying to get the selected item of a spinner created at the Settings class from the MainActivity class.

I used sharedpreferences within the Settings activity to save the selected spinner value when the activity closes. This works.

To get the value from Settings activity while in MainActivity I use

public static Settings getlang = new Settings();

and

getlang.getLang1().getItemAtPosition(getlang.getLang1().getSelectedItemPosition());

getLang1() is a methode I created in the Settings activity that returns the Spinner itself.

The problem is, If I try to do this without opening the Settings activity first and directly going into the MainActivity I get the NullPointerException at this line

 getlang.getLang1().getItemAtPosition(getlang.getLang1().getSelectedItemPosition());

But If I first open the Settings activity and then go to MainActivity everything works fine.

How can I fix this?

Here is the Settings activity

public class Settings extends Activity {

    public SharedPreferences prefsSet;
    public String prefNameSet = "MyPrefSet";

    public static final String PREFS_NAME_SET = "SAVEDATASET";

    private static final String SPINNER1_STATE = "spinner1_state";

    public int language;

    public int userChoice;

    private static Spinner spinner1;
    private Button savesett;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        savesett = (Button) findViewById(R.id.bSaveSett);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        SharedPreferences sharedPref = getSharedPreferences("FileName",
                MODE_PRIVATE);
        int spinnerValue = sharedPref.getInt("userChoiceSpinner", -1);
        if (spinnerValue != -1)
            // set the value of the spinner
            spinner1.setSelection(spinnerValue);

        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                // TODO Auto-generated method stub
                userChoice = spinner1.getSelectedItemPosition();
                SharedPreferences sharedPref = getSharedPreferences("FileName",
                        0);
                SharedPreferences.Editor prefEditor = sharedPref.edit();
                prefEditor.putInt("userChoiceSpinner", userChoice);
                prefEditor.commit();
                Toast.makeText(
                        parent.getContext(),
                        "Chosen Language: "
                                + parent.getItemAtPosition(pos).toString(),
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        savesett.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent ourIntent = new Intent(Settings.this, MainActivity.class);
                startActivity(ourIntent);
            }

        });
    }

    public int getLang() {

        return userChoice;}

        public Spinner getLang1() {

            return Settings.spinner1;
    }

The issue is that the Settings activity may or may not be instantiated. Given that it is your main activity that is trying to use it, it most likely never will be.

What you want to do is persist the value of the spinner in your preferences and not the index of the spinner item.

Consider the case of a user being able to use a spinner that selects how often they want a service to be checked by the app. The spinner choices are "1 minute", "5 minutes", "10 minutes", "15 minutes", "30 minutes", "1 hour".

When the preference is saved, we'll save 1, 5, 10, 15, 30, or 60.

The activity that uses this information will just get back an int that corresponds to how many minutes it should wait between refreshes.

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