我正在制作一个 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)