0

当这个线程运行时我遇到了一个问题,所以我的应用程序崩溃了。我真的不明白,问题出在哪里。

public void onClick(DialogInterface dialog, int which) {
    //Searching with post request           
    BugSenseHandler.sendEvent("Search started");
    new Thread(new Runnable() {
        @Override
        public void run() {
      try {
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

       nameValuePairs.add(new BasicNameValuePair("find", etMedName.getText().toString()));

       String prec = "";
       if (cbPrecise.isChecked()) prec = "on";
       else prec = "off";

       nameValuePairs.add(new BasicNameValuePair("exact_match", prec));

       HttpClient client = new DefaultHttpClient();
       HttpPost post = new HttpPost("http://mashkovsky.ru/tiki-listpages.php");
       post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
       HttpResponse resp = client.execute(post);

       String html = "";
       html = EntityUtils.toString(resp.getEntity());

       Document doc = Jsoup.parse(html, "http://mashkovsky.ru/tiki-listpages.php");
       Element content = doc.getElementById("wrapper");
       wView.loadData(content.outerHtml(), "text/html", "utf-8");
       MainActivity.this.removeDialog(SEARCH_DIALOG);   
     }
     catch (ClientProtocolException e) {e.printStackTrace();}
     catch (IOException e) {e.printStackTrace();}
}
}).start();             
}

我在 Eclipse 的 LogCat 窗口中没有堆栈跟踪,但BugSense显示了这个简短的堆栈跟踪

java.lang.NullPointerException
at ru.pathdevel.mashkovsyreference.MainActivity$1$1.run(MainActivity.java:106)
at java.lang.Thread.run(Thread.java:856)

UPD:问题解决了。一切都在初始化中。我试图采用 main.xml 布局小部件,而不是 searchdialog.xml

4

2 回答 2

1

只需在 Runnable 的 run 方法中放置一个断点,调试应用程序,检查变量的值。一个似乎是空的。

我会特别关注:

  • etMedName
  • cb精确

此外,您是否检查过您的 HttpResponse 是否包含内容?

顺便说一句,自 API 级别 13 起,removeDialog 已被弃用。我建议改用支持库中的 DialogFragment。

于 2013-07-24T08:47:37.993 回答
0

etMedName为空或etMedName.getText()为空。所以你可以做这样的事情:

@Override
public void run() {
    if(etMedName==null || etMedName.getText()==null) return;
    ...
}
于 2013-07-24T08:48:06.560 回答