简体   繁体   中英

Android run setup activity on first run only

I am working on my first android app and i have a spinner on my main activity and when the user runs the app there after the landing screen or activity will be a different one based (not main). I am storing the selection from the spinner in a shared preferance and if this is set I would like to load a different activity on startup.

Code

private Spinner spinner;
private SharedPreferences favTeam;
//private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /*
    if (favTeam.contains("team") && favTeam != null) {
        Intent intent = new Intent(this, MyTeamActivity.class);
        startActivity(intent);
    } else {
        System.out.println("-------- cant read from shared pref -----------");
    }

*/


    spinner = (Spinner) findViewById(R.id.selectTeam);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.teams_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

}


public void submitMyTeam(View v){
    favTeam = this.getSharedPreferences("com.lonerganonline.league_of_ireland", Context.MODE_PRIVATE);
    //SharedPreferences favTeam = this.getSharedPreferences("com.lonerganonline.league_of_ireland", Context.MODE_PRIVATE);
    TextView txt = (TextView) findViewById(R.id.textView);
    String selectedTeam = spinner.getSelectedItem().toString();

    SharedPreferences.Editor editor = favTeam.edit();
    editor.putString("team", selectedTeam);
    editor.apply();
    Intent intent = new Intent(this, MyTeamActivity.class);

    txt.setText(favTeam.getString("team","Not Defined"));

    startActivity(intent);

}

i tried doing it by the commented out if statement in the code above but that led to fatal errors. any help would be greatly appreciated.

Because you are trying to read values from favTeam without initializing it. that why its not working and you are getting NullPointerException .

You have to initialize favteam ShardePreference before reading values from it,

using

favTeam = this.getSharedPreferences("com.lonerganonline.league_of_ireland", Context.MODE_PRIVATE);

in onCreate() So your code look like,

 favTeam = this.getSharedPreferences("com.lonerganonline.league_of_ireland", Context.MODE_PRIVATE);

   if (favTeam.contains("team") && favTeam != null) {
        Intent intent = new Intent(this, MyTeamActivity.class);
        startActivity(intent);
    } else {
        System.out.println("-------- cant read from shared pref -----------");
    }

If you still get errors then post error log.

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