我正在尝试从网址获取HTML源代码,然后将其传递给字符串。 按下调试按钮后,它会询问我是否要打开debuger(代码编译正常。)
debuger: ActivityThread.performLaunchActivity(ActivityThread $ ActivityClientRecord,Intent)line:2180
说e>原因> “java.lang.NullPointerException”和“detailMessage null”。
所以我理解这是一个NullPointerException,但无法看到。
Amd是LogCat唯一说的是:
08-14 00:34:10.437: E/Trace(1784): error opening trace file: No such file or directory (2)
08-14 00:34:10.437: I/System.out(1784): Sending WAIT chunk
08-14 00:34:10.437: I/dalvikvm(1784): Debugger is active
08-14 00:34:10.644: I/System.out(1784): Debugger has connected
08-14 00:34:10.644: I/System.out(1784): waiting for debugger to settle...
08-14 00:34:11.867: I/System.out(1784): debugger has settled (1300)
08-14 00:34:13.428: D/dalvikvm(1784): threadid=1: still suspended after undo (sc=1 dc=1)
我真的不知道会给你更多信息。 :/
我使用的代码是(这是GetCode类)
public String html;
public void getSourceCode() throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.novasoftware.se/webviewer/(S(pisjjgujku50by55lpbdl1a2))/design1.aspx?schoolid=18200&code=83310");
HttpResponse response = client.execute(request);
html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
System.out.println("html");
}
然后在主类我尝试打印代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
doStuff();
}
private void doStuff() {
try {
getcode.getSourceCode();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(getcode.html);
}
答案 0 :(得分:0)
首先,您仍未提供足够的信息以进行准确诊断。你应该提供:
话虽如此,我可以在您提供的(部分)代码中看到NPE的两个可能原因:
如果未调用getSourceCode()
,则在调用html
方法时,null
变量可能为onCreate
。
如果client.execute(request)
返回的Response对象是错误响应(即状态是4xx或5xx响应代码),则response.getEntity()
将返回null
。我认为如果回复没有内容,也会发生这种情况。