0

我正在制作一个 android 应用程序,我想在其中使用来自 SQLServer 2008 的字符串值数组填充微调器。我遇到一个问题,它说微调器无法从与主线程不同的线程中填充。

       Thread waitth = new Thread() {
         public void run() {
             try {
                    System.out.println("3");

                 String details="mani";
                        System.out.println("4");
                        URLConnection con = getServletConnection();
                        System.out.println("5");
                        ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream());
                        System.out.println("7");
                        oos.writeObject(details);
                        System.out.println("8");
                        oos.flush();
                        oos.close();
                        // receive result from servlet
                        InputStream inputStream = con.getInputStream();
                        ObjectInputStream inputFromServlet = new ObjectInputStream(
                                inputStream);
                        result = (String[]) inputFromServlet.readObject();
                        //System.out.println(result);
                        inputFromServlet.close();
                        inputStream.close();
                        System.out.println();
                        System.out.println("got it");

                    System.out.println("450200");


                    ArrayList<String> aus=new ArrayList<String>();
                    for(int i=0;i<result.length;i++)
                    aus.add(result[i]);
                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, aus);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                   s1=(Spinner)findViewById(R.id.spinner1);

                        s1.setAdapter(adapter);




             }  
            catch (Exception ex) {
              ex.printStackTrace();
            }
         }
            };
            waitth.start();


    }


    private URLConnection getServletConnection() throws MalformedURLException,
       IOException {
    URL urlServlet = new URL("http://ipaddress/spinnerfordata");//servlet
    URLConnection con = urlServlet.openConnection();
    con.setUseCaches (false); 
       con.setDefaultUseCaches(false);
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type","application/x-java-serialized-object");
    System.out.println("9");
    return con;

  }

日志错误:

      08-02 18:49:08.439: W/System.err(918):                      android.view.ViewRootImpl$CalledFromWrongThreadException  : Only the original thread that created a view hierarchy can touch its views.
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2588)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
08-02 18:49:08.454: W/System.err(918):  at android.view.View.setFlags(View.java:8412)
08-02 18:49:08.464: W/System.err(918):  at android.view.View.setFocusable(View.java:5769)
08-02 18:49:08.464: W/System.err(918):  at android.widget.AdapterView.checkFocus(AdapterView.java:718)
08-02 18:49:08.464: W/System.err(918):  at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:115)
08-02 18:49:08.464: W/System.err(918):  at android.widget.Spinner.setAdapter(Spinner.java:380)
08-02 18:49:08.464: W/System.err(918):  at com.example.manispinner.MainActivity$1.run(MainActivity.java:73)
4

2 回答 2

0

您正在尝试从非 UI 线程更新 UI,但您不能按照错误所述执行此操作。与 UI 相关的任何事情都需要在 UI 线程上完成

于 2013-08-02T18:50:49.443 回答
0
  1. 使用 API 从服务器获取数据,xml或者json
  2. 解析数据。
  3. 将其存储在一个数组中。

    Do all the above in AsyncTask.
    
  4. 最后将其填充到微调器。

在 GitHub Spinner-Dynamic中查找示例项目

于 2013-08-02T18:59:55.793 回答