所以我正在研究一个从URL读取XML文件的应用程序。我发现没有找到任何来源的问题。不断弹出的文件是
activitythread.performlaunchactivity(activitythread $ activityclientrecord intent)行:2305
这是我的代码:
package com.lnrpuc.lnrpucseniordesign;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
public class LightsMainScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lights_main);
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = pullParserFactory.newPullParser();
URL url = new URL("http://lnrpuc.com/List.xml");
URLConnection urlc = url.openConnection();
InputStream is = new BufferedInputStream(urlc.getInputStream());
xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
xpp.setInput(is,null);
parseXML(xpp);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Button returnBtn = (Button)findViewById(R.id.returnBtn);
returnBtn.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
private void parseXML(XmlPullParser xpp) throws XmlPullParserException,IOException
{
ArrayList<Lights> lights = null;
int eventType = xpp.getEventType();
Lights lightStatus = null;
boolean lightsTag = false;
boolean lightsStatusTag = false;
while(eventType != XmlPullParser.END_DOCUMENT) {
String currentTag = null;
switch(eventType){
case XmlPullParser.START_DOCUMENT:
lights = new ArrayList<Lights>();
break;
case XmlPullParser.START_TAG:
currentTag = xpp.getName();
if(currentTag == "Lights"){
lightStatus = new Lights();
lightsTag = true; }
else if(currentTag == "Status"){
lightsStatusTag = true; }
else if(currentTag == "a" && lightsTag && lightsStatusTag) {
lightStatus.light1 = xpp.nextText(); }
else if(currentTag == "b" && lightsTag && lightsStatusTag) {
lightStatus.light2 = xpp.nextText(); }
else if(currentTag == "c" && lightsTag && lightsStatusTag) {
lightStatus.light3 = xpp.nextText(); }
else if(currentTag == "d" && lightsTag && lightsStatusTag) {
lightStatus.light4 = xpp.nextText(); }
else if(currentTag == "e" && lightsTag && lightsStatusTag) {
lightStatus.light5 = xpp.nextText(); }
else if(currentTag == "f" && lightsTag && lightsStatusTag) {
lightStatus.light6 = xpp.nextText(); }
else if(currentTag == "g" && lightsTag && lightsStatusTag) {
lightStatus.light7 = xpp.nextText(); }
else if(currentTag == "h" && lightsTag && lightsStatusTag) {
lightStatus.light8 = xpp.nextText(); }
break;
case XmlPullParser.END_TAG:
currentTag = xpp.getName();
if(currentTag.equalsIgnoreCase("status") && lightStatus != null){
lights.add(lightStatus);
lightsStatusTag = false; }
else if(currentTag.equalsIgnoreCase("lights")){
lightsTag = false; }
}
eventType = xpp.next();
}
setLights(lights);
}
private void setLights(ArrayList<Lights> lights)
{
int light_1 = 0, light_2 = 0, light_3 = 0, light_4 = 0,
light_5 = 0, light_6 = 0, light_7 = 0, light_8 = 0;
ToggleButton light1Btn = (ToggleButton)findViewById(R.id.light_1);
ToggleButton light2Btn = (ToggleButton)findViewById(R.id.light_2);
ToggleButton light3Btn = (ToggleButton)findViewById(R.id.light_3);
ToggleButton light4Btn = (ToggleButton)findViewById(R.id.light_4);
ToggleButton light5Btn = (ToggleButton)findViewById(R.id.light_5);
ToggleButton light6Btn = (ToggleButton)findViewById(R.id.light_6);
ToggleButton light7Btn = (ToggleButton)findViewById(R.id.light_7);
ToggleButton light8Btn = (ToggleButton)findViewById(R.id.light_8);
Iterator<Lights> it = lights.iterator();
while(it.hasNext())
{
Lights lightStatus = (Lights) it.next();
light_1 = Integer.parseInt(lightStatus.light1);
light_2 = Integer.parseInt(lightStatus.light2);
light_3 = Integer.parseInt(lightStatus.light3);
light_4 = Integer.parseInt(lightStatus.light4);
light_5 = Integer.parseInt(lightStatus.light5);
light_6 = Integer.parseInt(lightStatus.light6);
light_7 = Integer.parseInt(lightStatus.light7);
light_8 = Integer.parseInt(lightStatus.light8);
}
if(light_1 == 1) {
light1Btn.setChecked(true);
} else light1Btn.setChecked(false);
if(light_2 == 1) {
light2Btn.setChecked(true);
} else light2Btn.setChecked(false);
if(light_3 == 1) {
light3Btn.setChecked(true);
} else light3Btn.setChecked(false);
if(light_4 == 1) {
light4Btn.setChecked(true);
} else light4Btn.setChecked(false);
if(light_5 == 1) {
light5Btn.setChecked(true);
} else light5Btn.setChecked(false);
if(light_6 == 1) {
light6Btn.setChecked(true);
} else light6Btn.setChecked(false);
if(light_7 == 1) {
light7Btn.setChecked(true);
} else light7Btn.setChecked(false);
if(light_8 == 1) {
light8Btn.setChecked(true);
} else light8Btn.setChecked(false);
}
}
class Lights
{
public String light1;
public String light2;
public String light3;
public String light4;
public String light5;
public String light6;
public String light7;
public String light8;
}
我是Android编程新手,不知道问题出在哪里。
02-01 22:48:10.628: W/ActivityThread(14211): Application com.lnrpuc.lnrpucseniordesign is waiting for the debugger on port 8100...
02-01 22:48:10.628: I/System.out(14211): Sending WAIT chunk
02-01 22:48:10.768: I/dalvikvm(14211): Debugger is active
02-01 22:48:10.828: I/System.out(14211): Debugger has connected
02-01 22:48:10.828: I/System.out(14211): waiting for debugger to settle...
02-01 22:48:11.028: I/System.out(14211): waiting for debugger to settle...
02-01 22:48:11.239: I/System.out(14211): waiting for debugger to settle...
02-01 22:48:11.439: I/System.out(14211): waiting for debugger to settle...
02-01 22:48:11.639: I/System.out(14211): waiting for debugger to settle...
02-01 22:48:11.839: I/System.out(14211): waiting for debugger to settle...
02-01 22:48:12.039: I/System.out(14211): waiting for debugger to settle...
02-01 22:48:12.230: I/System.out(14211): debugger has settled (1310)
02-01 22:48:19.597: D/AndroidRuntime(14211): Shutting down VM
02-01 22:48:19.597: W/dalvikvm(14211): threadid=1: thread exiting with uncaught exception (group=0x417c0898)
02-01 22:48:19.658: E/AndroidRuntime(14211): FATAL EXCEPTION: main
02-01 22:48:19.658: E/AndroidRuntime(14211): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lnrpuc.lnrpucseniordesign/com.lnrpuc.lnrpucseniordesign.LightsMainScreen}: android.os.NetworkOnMainThreadException
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.app.ActivityThread.access$700(ActivityThread.java:165)
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.os.Looper.loop(Looper.java:137)
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.app.ActivityThread.main(ActivityThread.java:5455)
02-01 22:48:19.658: E/AndroidRuntime(14211): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 22:48:19.658: E/AndroidRuntime(14211): at java.lang.reflect.Method.invoke(Method.java:525)
02-01 22:48:19.658: E/AndroidRuntime(14211): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-01 22:48:19.658: E/AndroidRuntime(14211): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-01 22:48:19.658: E/AndroidRuntime(14211): at dalvik.system.NativeStart.main(Native Method)
02-01 22:48:19.658: E/AndroidRuntime(14211): Caused by: android.os.NetworkOnMainThreadException
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
02-01 22:48:19.658: E/AndroidRuntime(14211): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-01 22:48:19.658: E/AndroidRuntime(14211): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-01 22:48:19.658: E/AndroidRuntime(14211): at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
02-01 22:48:19.658: E/AndroidRuntime(14211): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
02-01 22:48:19.658: E/AndroidRuntime(14211): at com.lnrpuc.lnrpucseniordesign.LightsMainScreen.onCreate(LightsMainScreen.java:38)
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.app.Activity.performCreate(Activity.java:5372)
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-01 22:48:19.658: E/AndroidRuntime(14211): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
02-01 22:48:19.658: E/AndroidRuntime(14211): ... 11 more
更新: 我改为使用AsyncTask而不是之前的配置,我现在收到线程错误。我不确定为什么会抛出这些错误。
LightsMainScreen.java
package com.lnrpuc.lnrpucseniordesign;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;
import com.lnrpuc.lnrpucseniordesign.LightTest1;
public class LightsMainScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lights_main);
Button returnBtn = (Button)findViewById(R.id.returnBtn);
ToggleButton light3Btn = (ToggleButton)findViewById(R.id.light_3);
light3Btn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
new UpdateXML().execute(); } } ) ;
returnBtn.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
finish(); } } ) ;
}
private class UpdateXML extends AsyncTask<String, Void, Void>
{
ToggleButton light3Btn = (ToggleButton)findViewById(R.id.light_3);
protected Void doInBackground(String... params) {
if(light3Btn.isChecked()) LightTest1.XMLUpdate("1", "3", "1");
else LightTest1.XMLUpdate("1", "3", "0");
return null;
}
protected void onPostExecute() {}
@Override
protected void onPreExecute() {}
protected void onProgressUpdate() {}
}
}
LightTest1.java
package com.lnrpuc.lnrpucseniordesign;
import java.rmi.RemoteException;
import com.lnrpuc.Services.GetNamesRequestType;
import com.lnrpuc.Services.GetNamesResponseType;
import com.lnrpuc.Services.GetStatusRequestType;
import com.lnrpuc.Services.GetStatusResponseType;
import com.lnrpuc.Services.HomeAutomationServicePortTypeProxy;
import com.lnrpuc.Services.SetStatusRequestType;
import com.lnrpuc.Services.SetStatusResponseType;
public class LightTest1 {
public static void XMLUpdate(String object, String number, String status){
try {
HomeAutomationServicePortTypeProxy proxy = new HomeAutomationServicePortTypeProxy();
//names request
//GetNamesRequestType inGNRT = new GetNamesRequestType();
//inGNRT.setPass("12345");
//GetNamesResponseType outGNRT = proxy.getNames(inGNRT);
//System.out.println(outGNRT.get_return());
//status request
//GetStatusRequestType inGSRT = new GetStatusRequestType();
//inGSRT.setPass("12345");
//GetStatusResponseType outGSRT = proxy.getStatus(inGSRT);
//System.out.println(outGSRT.get_return());
//set status
SetStatusRequestType inSSRT = new SetStatusRequestType();
inSSRT.setPass("12345");
inSSRT.setObject(object);
inSSRT.setNumber(number);
inSSRT.setStatus(status);
SetStatusResponseType outSSRT = proxy.setStatus(inSSRT);
System.out.println(outSSRT.get_return());
} catch (RemoteException e) {
// TODO Auto-generated catch block
System.out.println("failed");
}
}
}
LogCat输出
02-06 13:53:38.759: I/dalvikvm(16012): Failed resolving Lcom/lnrpuc/Services/HomeAutomationServicePortType; interface 93 'Ljava/rmi/Remote;'
02-06 13:53:38.759: W/dalvikvm(16012): Link of class 'Lcom/lnrpuc/Services/HomeAutomationServicePortType;' failed
02-06 13:53:38.759: I/dalvikvm(16012): Failed resolving Lcom/lnrpuc/Services/HomeAutomationServicePortTypeProxy; interface 30 'Lcom/lnrpuc/Services/HomeAutomationServicePortType;'
02-06 13:53:38.759: W/dalvikvm(16012): Link of class 'Lcom/lnrpuc/Services/HomeAutomationServicePortTypeProxy;' failed
02-06 13:53:38.759: E/dalvikvm(16012): Could not find class 'com.lnrpuc.Services.HomeAutomationServicePortTypeProxy', referenced from method com.lnrpuc.lnrpucseniordesign.LightTest1.XMLUpdate
02-06 13:53:38.759: W/dalvikvm(16012): VFY: unable to resolve new-instance 31 (Lcom/lnrpuc/Services/HomeAutomationServicePortTypeProxy;) in Lcom/lnrpuc/lnrpucseniordesign/LightTest1;
02-06 13:53:38.759: D/dalvikvm(16012): VFY: replacing opcode 0x22 at 0x0000
02-06 13:53:38.759: W/dalvikvm(16012): VFY: unable to resolve exception class 94 (Ljava/rmi/RemoteException;)
02-06 13:53:38.759: W/dalvikvm(16012): VFY: unable to find exception handler at addr 0x26
02-06 13:53:38.759: W/dalvikvm(16012): VFY: rejected Lcom/lnrpuc/lnrpucseniordesign/LightTest1;.XMLUpdate (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
02-06 13:53:38.759: W/dalvikvm(16012): VFY: rejecting opcode 0x0d at 0x0026
02-06 13:53:38.769: W/dalvikvm(16012): VFY: rejected Lcom/lnrpuc/lnrpucseniordesign/LightTest1;.XMLUpdate (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
02-06 13:53:38.769: W/dalvikvm(16012): Verifier rejected class Lcom/lnrpuc/lnrpucseniordesign/LightTest1;
02-06 13:53:38.769: W/dalvikvm(16012): threadid=11: thread exiting with uncaught exception (group=0x417c0898)
02-06 13:53:38.769: E/AndroidRuntime(16012): FATAL EXCEPTION: AsyncTask #1
02-06 13:53:38.769: E/AndroidRuntime(16012): java.lang.RuntimeException: An error occured while executing doInBackground()
02-06 13:53:38.769: E/AndroidRuntime(16012): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-06 13:53:38.769: E/AndroidRuntime(16012): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-06 13:53:38.769: E/AndroidRuntime(16012): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-06 13:53:38.769: E/AndroidRuntime(16012): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-06 13:53:38.769: E/AndroidRuntime(16012): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-06 13:53:38.769: E/AndroidRuntime(16012): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-06 13:53:38.769: E/AndroidRuntime(16012): at java.lang.Thread.run(Thread.java:841)
02-06 13:53:38.769: E/AndroidRuntime(16012): Caused by: java.lang.VerifyError: com/lnrpuc/lnrpucseniordesign/LightTest1
02-06 13:53:38.769: E/AndroidRuntime(16012): at com.lnrpuc.lnrpucseniordesign.LightsMainScreen$UpdateXML.doInBackground(LightsMainScreen.java:41)
02-06 13:53:38.769: E/AndroidRuntime(16012): at com.lnrpuc.lnrpucseniordesign.LightsMainScreen$UpdateXML.doInBackground(LightsMainScreen.java:1)
02-06 13:53:38.769: E/AndroidRuntime(16012): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-06 13:53:38.769: E/AndroidRuntime(16012): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-06 13:53:38.769: E/AndroidRuntime(16012): ... 3 more
答案 0 :(得分:2)
您正在主UI线程上进行网络操作,这就是您的应用崩溃的原因。不要在onCreate方法中进行网络操作。
创建异步任务并在后台线程中执行网络操作
查看这些链接以获取更多信息
How to use AsyncTask correctly in Android
http://developer.android.com/reference/android/os/AsyncTask.html