如何在Android窗体中的spinner中添加值,并且数组连接到远程主机中的文本文件? 这段代码打印FATAL EXCEPTION:主....你可以帮我吗? 任何帮助将不胜感激.............
03-09 23:29:23.321: E/AndroidRuntime(3204): FATAL EXCEPTION: main
03-09 23:29:23.321: E/AndroidRuntime(3204): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxx.xxxxx/com.xxxxx.xxxxx.SpinnerPopText}: android.os.NetworkOnMainThreadException
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.access$700(ActivityThread.java:140)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.os.Looper.loop(Looper.java:137)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.main(ActivityThread.java:4935)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 23:29:23.321: E/AndroidRuntime(3204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-09 23:29:23.321: E/AndroidRuntime(3204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-09 23:29:23.321: E/AndroidRuntime(3204): at dalvik.system.NativeStart.main(Native Method)
03-09 23:29:23.321: E/AndroidRuntime(3204): Caused by: android.os.NetworkOnMainThreadException
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-09 23:29:23.321: E/AndroidRuntime(3204): at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
03-09 23:29:23.321: E/AndroidRuntime(3204): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
03-09 23:29:23.321: E/AndroidRuntime(3204): at com.xxxxx.xxxxx.SpinnerPopText.onCreate(SpinnerPopText.java:37)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.Activity.performCreate(Activity.java:5206)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-09 23:29:23.321: E/AndroidRuntime(3204): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
03-09 23:29:23.321: E/AndroidRuntime(3204): ... 11 more
java class
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.app.Activity;
public class SpinnerPopText extends Activity {
Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerpoptext);
String path = "http://www.myhost.com/public/country.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u
.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer);
bo.write(buffer);
final List<String> list=new ArrayList<String>();
list.add(bo.toString());
final Spinner sp=(Spinner) findViewById(R.id.my_spinner);
ArrayAdapter<String> adp= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adp);
(android.R.layout.simple_spinner_dropdown_item);
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
你不应该在主线程上做网络事情。
所以要么使用AsyncTask并在doinbackground上做你的工作,那么在postExecute中更新你的微调器。
或者使用线程进行网络工作,然后在线程调用runOnUIThread中进行操作。