我有一个基本的应用程序,它使用后台线程将值发布到我们的服务器。代码如下。当我运行代码时,在调试或正常情况下,以下行抛出NullException(尽管在调试时,设置了backgroundThread变量的值)。每一次后续运行都很好。我是Android开发的新手,我不确定导致这种差异的原因。我已经包含了活动功能和服务器发布功能。
backgroundThread.start();
这一行抛出了NullException。关于导致问题的原因是什么?感谢您提供的任何帮助。
- UPDATE @ jonas452& @Rudi
我更改了代码以使用asynctask并且正在努力使用返回值。 postResult总是为null,并且postTo函数中的断点似乎永远不会运行。可能是一个愚蠢的错误,但我不确定什么是错。
- UPDATE 我解决了这个问题。 AsyncTask设置正确,但由于它是异步的,因此postResult不能立即使用。我将支票移到了AsyncTask的onPostExecute函数中,一切正常。
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
try {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
final String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
scan_format.setText("FORMAT: " + scanFormat);
scan_content.setText("CONTENT: " + scanContent);
error.setText(null);
output.setText(null);
neonetpost post = new neonetpost();
post.execute(scanContent);
/*
Thread backgroundThread = new Thread(new Runnable() {
@Override
public void run() {
postResult = postTo(scanContent);
}
});
backgroundThread.start();
*/
if(postResult.indexOf("ERROR: ")>-1)
error.setText(postResult);
else
output.setText(postResult);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
error.setText("ERROR: " + e.getMessage());
}
}
public class neonetpost extends AsyncTask<String,String,String>
{
@Override
protected void onPreExecute() {
}
protected void onPostExecute(String result) {
}
protected String doInBackground(String... params) {
return postTo(params[0], params[1]);
}
public String postTo(String operation) {
return postTo(operation, "E:1");
}
public String postTo(String operation, String employee)
{
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://domain.com/page.php");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("operation", operation));
nameValuePairs.add(new BasicNameValuePair("employee", employee));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// According to the JAVA API, InputStream constructor do nothing.
//So we can't initialize InputStream although it is not an interface
InputStream inputStream = response.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null){
stringBuilder.append(bufferedStrChunk);
}
postResult = stringBuilder.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
postResult = "ERROR: " + e.getMessage();
}
return postResult;
}
}
答案 0 :(得分:0)
不要使用Java Threads,请使用AsyncTask类。 这是一个很好的教程: http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html
答案 1 :(得分:0)
给你NPE的行是if(postResult.indexOf("ERROR: ")>-1)
。你假设调用
backgroundThread.start();
使UI线程等待,直到backgroundThread
完成执行并将值赋给postResult
,但Thread是异步执行的。和backgroundThread.start();
使系统产生一个新线程。执行此操作时,取决于调度程序