【问题标题】:Handling orientation changes with dynamic spinners使用动态微调器处理方向变化
【发布时间】:2012-07-05 12:49:29
【问题描述】:

我已经在网上搜索并尝试了 5 个小时,但我找不到解决问题的方法。我想在方向改变后保持我的微调器的状态。

我有 2 个动态创建的微调器。他们通过 http 请求获取项目,第一个微调器通过 setOnItemSelectedListener() 方法更改第二个中的项目。我将字符串读入全局列表。

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;
}

我的第一个问题是阻止活动重新启动。

android:configChanges="orientation|keyboardHidden"

我已在清单文件中进行了此更改,以自行处理方向更改并避免重新启动活动。

我找到了一种在互联网上处理方向变化的方法,但它对我不起作用。更改方向时出现空指针异常。

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

这是我到目前为止所做的。我什至不确定我正在尝试的方式。因此,任何更合适的方式将不胜感激。

@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

        }
    });
  }

【问题讨论】:

  • 如果您打印了 nullpointerexception 的整个堆栈跟踪,将会有所帮助。
  • 或许你应该看看onConfigChanged()方法。它让您可以非常轻松地处理配置更改。

标签: android spinner httprequest android-configchanges


【解决方案1】:

您需要先致电setContentView(R.layout.match_selection_layout),然后再致电findViewById。否则,findViewById 将返回 null(因为父级 View 尚未膨胀/附加到屏幕上)。

您也应该使用configChanges 作为修复您的活动的创可贴。 Activity 可以通过多种方式重新启动;例如,如果用户将设备的语言设置从英语更改为西班牙语(这将导致整个Activity 被销毁并创建)。始终确保方向更改正常工作...不要只是放弃它们并简单地覆盖onConfigurationChanged

【讨论】:

  • 设置内容视图似乎有效。但我认为我应该使用“public void onConfigurationChanged(Configuration newConfig)”方法。有什么有用的使用教程吗?
  • 您使用configChanges的原因是什么?我会尝试找到一个,但你应该有充分的理由在你这样做之前使用它......
  • 我只想处理方向变化。我需要根据方向更改屏幕显示。如果您问我为什么不禁止方向更改,我认为用户最好以他们想要的方式使用应用程序。
  • @Srht 你永远,永远,永远告诉任何人永远,不管我活多久,(ever, ever, ever) 禁止屏幕方向改变。我比什么都讨厌……这只是开发人员的懒惰,哈哈
  • 您可以做的是创建两个具有相同名称的独立布局(一个用于横向模式,一个用于纵向模式)。然后将纵向布局放入res/layout(这是默认的后备布局),将横向布局放入res/layout-land。这将确保在运行时选择正确的布局。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多