我正在为android做一个程序,检查我的数据库中是否存在名为sms的表中的行。如果是,则获取数字,文本和ID并发送消息。
但是,有一个我无法理解的错误。我使用此代码检查数据库:
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
TextView tres = (TextView) findViewById(R.id.txtresult);
final String TAG = "AsyncTaskParseJson.java";
// set your json string url here
String yourJsonStringUrl= "http://192.168.1.7/aplicacao_sms/load_sms.php";
//String yourJsonStringUrl = "http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php";
// contacts JSONArray
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray("products");
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
String id=c.getString("id");
String numero=c.getString("numero");
String msg=c.getString("msg");
send_sms(numero, msg, id);
}
dataJsonArr = json.getJSONArray("products");
JSONObject c = dataJsonArr.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) {}
}
当执行错误时:
08-17 14:13:39.993 7781-7794/pt.styledesign.outro_teste W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x40de22a0)
08-17 14:13:40.393 7781-7794/pt.styledesign.outro_teste E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5083)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:976)
at android.view.View.requestLayout(View.java:15520)
at android.view.View.requestLayout(View.java:15520)
at android.view.View.requestLayout(View.java:15520)
at android.view.View.requestLayout(View.java:15520)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
at android.view.View.requestLayout(View.java:15520)
at android.widget.ScrollView.requestLayout(ScrollView.java:1711)
at android.view.View.requestLayout(View.java:15520)
at android.widget.TextView.checkForResize(TextView.java:6677)
at android.widget.TextView.updateAfterEdit(TextView.java:7575)
at android.widget.TextView.handleTextChanged(TextView.java:7585)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9371)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:964)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:498)
at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:253)
at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
at android.widget.TextView.append(TextView.java:3367)
at android.widget.TextView.append(TextView.java:3354)
at pt.styledesign.outro_teste.MainActivity.send_sms(MainActivity.java:189)
at pt.styledesign.outro_teste.MainActivity.access$000(MainActivity.java:23)
at pt.styledesign.outro_teste.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:87)
at pt.styledesign.outro_teste.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:47)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
任何人都可以帮助我吗?
答案 0 :(得分:5)
您的问题是由此引起的:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
您无法从doInBackground()方法访问任何UI元素。安全的方法是使用方法publishProgress()
和onPostExecute()
。
因此,您可能需要将send_sms()
中的参数包装到新对象中,在publishProgress()
中发布,然后您可以在TextView tres
方法上更新onProgressUpdate()
。
答案 1 :(得分:1)
您可以获得此异常,因为您正在使用doInBackground()方法更新UI。
像这样更改你的AsyncTask。
public class AsyncTaskParseJson extends AsyncTask<String, Void, JSONObject> {
TextView tres = (TextView) findViewById(R.id.txtresult);
final String TAG = "AsyncTaskParseJson.java";
// set your json string url here
String yourJsonStringUrl= "http://192.168.1.7/aplicacao_sms/load_sms.php";
//String yourJsonStringUrl = "http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php";
// contacts JSONArray
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected JSONObject doInBackground(String... arg0) {
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
return json;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
// get the array of users
dataJsonArr = json.getJSONArray("products");
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
String id=c.getString("id");
String numero=c.getString("numero");
String msg=c.getString("msg");
send_sms(numero, msg, id);
}
dataJsonArr = json.getJSONArray("products");
JSONObject c = dataJsonArr.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
我希望它有所帮助!