简体   繁体   English

使用动态微调器处理方向变化

[英]Handling orientation changes with dynamic spinners

I have searched the web and keep trying for 5 hours but I couldn't find a way to solve the problem. 我已经在网上搜索并尝试了5个小时,但找不到解决问题的方法。 I want to keep the state of my spinners after orientation changes. 方向更改后,我想保持微调器的状态。

I have 2 spinners which are created dynamically. 我有2个动态创建的微调器。 They get their items by http request and the first spinner changes the items in the second one by setOnItemSelectedListener() method. 他们通过http请求获取项目,第一个微调器通过setOnItemSelectedListener()方法更改第二个项目。 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. 我找到了一种方法来处理Internet上的方向更改,但对我不起作用。 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

        }
    });
  }

You need to call setContentView(R.layout.match_selection_layout) before calling findViewById . 您需要先调用setContentView(R.layout.match_selection_layout)然后再调用findViewById Otherwise, findViewById will return null (since the parent View has not yet been inflated/attached to the screen). 否则, findViewById将返回null (因为父View尚未被放大/附加到屏幕上)。

Also you should not use configChanges as a bandaid to fix your activity. 此外,你应该使用configChanges作为绷带来固定你的活动。 There are many ways an Activity can be restarted; 重新启动Activity方法有很多。 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). 例如,如果用户将设备的语言设置从英语更改为西班牙语(这将导致整个Activity被销毁和创建)。 Always make sure orientation changes work correctly... don't just give up on them and simply override onConfigurationChanged . 始终确保方向更改正确起作用...不要仅仅放弃它们,而只需重写onConfigurationChanged

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

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