Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling orientation changes with dynamic spinners
    primarykey
    data
    text
    <p>I have searched the web and keep trying for 5 hours but I couldn't find a way to solve the problem. I want to keep the state of my spinners after orientation changes.</p> <p>I have 2 spinners which are created dynamically. They get their items by http request and the first spinner changes the items in the second one by setOnItemSelectedListener() method. I read the Strings into Global Lists.</p> <pre><code>public class Global { public static String userName; public static String userType; public static String serverIp; public static int spinnerLeaguePos=0; public static int spinnerMatchPos=0; public static List&lt;String&gt; leagues; public static List&lt;String&gt; matches; } </code></pre> <p>My first problem was preventing the activity from restart.</p> <pre><code>android:configChanges="orientation|keyboardHidden" </code></pre> <p>I have made this change in my manifest file to handle orientation changes by myself and avoid activity from restart.</p> <p>I have found a way for handling orientation changes on internet but it doesn't worked for me. I'm getting null pointer exception when I change the orientation.</p> <pre><code>07-05 12:41:08.119: E/AndroidRuntime(26388): FATAL EXCEPTION: main 07-05 12:41:08.119: E/AndroidRuntime(26388): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MatchSelectionActivity}: java.lang.NullPointerException 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.app.ActivityThread.access$700(ActivityThread.java:123) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.os.Handler.dispatchMessage(Handler.java:99) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.os.Looper.loop(Looper.java:137) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.app.ActivityThread.main(ActivityThread.java:4424) 07-05 12:41:08.119: E/AndroidRuntime(26388): at java.lang.reflect.Method.invokeNative(Native Method) 07-05 12:41:08.119: E/AndroidRuntime(26388): at java.lang.reflect.Method.invoke(Method.java:511) 07-05 12:41:08.119: E/AndroidRuntime(26388): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 07-05 12:41:08.119: E/AndroidRuntime(26388): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 07-05 12:41:08.119: E/AndroidRuntime(26388): at dalvik.system.NativeStart.main(Native Method) 07-05 12:41:08.119: E/AndroidRuntime(26388): Caused by: java.lang.NullPointerException 07-05 12:41:08.119: E/AndroidRuntime(26388): at com.example.MatchSelectionActivity.setLeagues(MatchSelectionActivity.java:90) 07-05 12:41:08.119: E/AndroidRuntime(26388): at com.example.MatchSelectionActivity.onCreate(MatchSelectionActivity.java:47) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.app.Activity.performCreate(Activity.java:4465) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 07-05 12:41:08.119: E/AndroidRuntime(26388): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 07-05 12:41:08.119: E/AndroidRuntime(26388): ... 12 more </code></pre> <p>This is what I have done so far. I don't even sure about to way I'm trying to do it. So any more appropriate way for doing this will be appreciated.</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); spinnerLeague = (Spinner) findViewById(R.id.spinner_league); spinnerMatch = (Spinner) findViewById(R.id.spinner_match); if (savedInstanceState != null) { setLeagues(); setMatches(); spinnerLeague.setSelection(Global.spinnerLeaguePos); spinnerMatch.setSelection(Global.spinnerMatchPos); } else{ Global.leagues = new ArrayList&lt;String&gt;(); Global.matches = new ArrayList&lt;String&gt;(); Global.leagues.add(getString(R.string.league_select)); Global.matches.add(getString(R.string.match_select)); GetLeagues task = new GetLeagues(); String requestString = "http://" + Global.serverIp + ":8080/server/GetCurrentLeagues"; task.execute(new String[] { requestString }); } setContentView(R.layout.match_selection_layout); } public class GetLeagues extends AsyncTask&lt;String, Void, String[]&gt; { @Override protected String[] doInBackground(String... urls) { //this part works properly, and reads the leagues into Global.leagues List } @Override protected void onPostExecute(String[] result) { setLeagues(); } } public class GetMatches extends AsyncTask&lt;String, Void, String[]&gt; { @Override protected String[] doInBackground(String... urls) { //this part works properly, and reads the matches into Global.matches List } @Override protected void onPostExecute(String[] result) { setMatches(); } } public void setLeagues() { spinnerLeague = (Spinner) findViewById(R.id.spinner_league); ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_spinner_item, Global.leagues); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerLeague.setAdapter(dataAdapter); spinnerLeague.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) { spinnerLeague = (Spinner) findViewById(R.id.spinner_league); if(position!=0){ spinnerMatch.setEnabled(true); Global.spinnerLeaguePos=position; GetMatches task = new GetMatches(); String requestString = "http://" + Global.serverIp +":8080/server/GetCurrentMatches/"+Global.spinnerLeaguePos; task.execute(new String[] { requestString }); } else{ setMatches(); spinnerMatch.setEnabled(false); Global.spinnerLeaguePos=position; } } public void onNothingSelected(AdapterView&lt;?&gt; parent) { // TODO Auto-generated method stub } }); } public void setMatches() { spinnerMatch = (Spinner) findViewById(R.id.spinner_match); ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_spinner_item, Global.matches); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerMatch.setAdapter(dataAdapter); spinnerMatch.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) { Global.spinnerMatchPos=position; } public void onNothingSelected(AdapterView&lt;?&gt; parent) { // TODO Auto-generated method stub } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload