简体   繁体   English

仅在首次运行时执行Android运行设置活动

[英]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). 我正在开发我的第一个android应用程序,并且我的主要活动上有一个微调器,当用户在着陆屏幕或活动后在那里运行该应用程序时,它将基于其他应用程序(不是主要的)。 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. 我尝试通过上面代码中的if语句注释掉它,但是导致致命错误。 any help would be greatly appreciated. 任何帮助将不胜感激。

Because you are trying to read values from favTeam without initializing it. 因为您正在尝试从favTeam读取值而不进行初始化。 that why its not working and you are getting NullPointerException . 那为什么它不起作用,而您却得到NullPointerException

You have to initialize favteam ShardePreference before reading values from it, 您必须先初始化favteam ShardePreference然后favteam ShardePreference读取值,

using 使用

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

in onCreate() So your code look like, onCreate() ,您的代码如下所示:

 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. 如果仍然出现错误,请发布错误日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM