Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8187305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:31:03+00:00 2026-06-07T02:31:03+00:00

I have searched the web and keep trying for 5 hours but I couldn’t

  • 0

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.

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.

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<String> leagues;
    public static List<String> matches;
}

My first problem was preventing the activity from restart.

android:configChanges="orientation|keyboardHidden"

I have made this change in my manifest file to handle orientation changes by myself and avoid activity from restart.

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.

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

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.

@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<String>();
        Global.matches = new ArrayList<String>();

        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<String, Void, String[]> {
    @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<String, Void, String[]> {
    @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<String> dataAdapter = new ArrayAdapter<String>(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<?> 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<?> parent) {
            // TODO Auto-generated method stub

        }
    });
  }

    public void setMatches() {

    spinnerMatch = (Spinner) findViewById(R.id.spinner_match);
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(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<?> parent, View view, int position, long id) {
            Global.spinnerMatchPos=position;
        }

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

        }
    });
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:31:04+00:00Added an answer on June 7, 2026 at 2:31 am

    You need to call setContentView(R.layout.match_selection_layout) before calling findViewById. Otherwise, findViewById will return null (since the parent View has not yet been inflated/attached to the screen).

    Also you should not use configChanges as a bandaid to fix your activity. There are many ways an Activity can be restarted; for example, if the user changed the device’s language setting from English to Spanish (this would cause the entire Activity to be destroyed and created). Always make sure orientation changes work correctly… don’t just give up on them and simply override onConfigurationChanged.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched the web but I cannot find the solution to my problem.
I have searched the web but couldn't find any detailed idea about Native OS
I have searched the web for hours but I can not find anything about
I want to install rsync on windows xp. I have searched the web, but
I have searched the web for a way to do this, but perhaps I'm
I have searched the web for days now but I can't seem to find
I have searched web for libCURL bindings for Go language, but failed to find
I have searched the web but could not find an answer. how do I
Okay, I have literally searched all over the web, but I didn't find what
I have searched the web in attempt to work this problem out but to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.