Reputation: 71
I'm doing a program for android that check in my database if exists rows in a table named sms. If so, then get the number, the text and the id and send the message.
However, there is an error that I can not understand. I'm using this code to check on the database:
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) {}
}
and when executing that's the error:
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)
can anyone help me?
Upvotes: 2
Views: 137
Reputation: 12861
You can get this exception because Your are updating your UI in doInBackground() method.
Change your AsyncTask like this.
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();
}
}
}
I hope it helps!
Upvotes: 1
Reputation: 4036
Your problem is caused by this:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
You can't access any UI element from the doInBackground() method. The safe way to do this is by using the methods publishProgress()
and onPostExecute()
.
So you will probably need to wrap the parameters from send_sms()
into a new object, post it in publishProgress()
and then you can update TextView tres
on the onProgressUpdate()
method.
Upvotes: 5