Issue
Right I think as far as this goes you should consider me a complete noob, I am in way over my head.
I have attempted to cobble what I understand about this together from several partial tutorials each dealing with a different aspect that I want to do.
Long and short of it, all I want to do is get text read from a URL to display in a text view, my understanding is that I need an AsyncTask to do that since the 3.0 update.
Any help on where I am going wrong would be great as I don't understand what's causing the error and how to fix.
I do know that the code contained within the try and catch of the doInBackground works on its own before the 3.0 update. Whether this too needs to be modified to work with an Async however I don't know.
I also imagine that other issues will probably arise once this one is sorted, if anyone spots something glaringly obvious I would be grateful if you could point it out.
String Event;
TextView eventText;
TextView titleText;
String HTML;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newevent);
titleText = (TextView) findViewById(R.id.Title);
eventText = (TextView) findViewById(R.id.Event);
new eventupdate().execute();
}
public class eventupdate extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... url) {
try {
Thread.sleep(4000);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(
"http://masterzangetsu.eu/Apps/rocksoctest"); // URL!
HttpResponse response = httpClient.execute(httpGet,
localContext);
String result = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String line = null;
while ((line = reader.readLine()) != null) {
result += line + "\n";
HTML = result;
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
eventText.setText("" + HTML);
// TODO Auto-generated method stub
return null;
}
}
And the LogCat error is:
W/dalvikvm(394): threadid=9: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime(394): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(394): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(394): at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(394): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(394): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(394): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(394): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(394): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(394): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(394): at java.lang.Thread.run(Thread.java:1019)
E/AndroidRuntime(394): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
E/AndroidRuntime(394): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
E/AndroidRuntime(394): at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
E/AndroidRuntime(394): at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394): at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394): at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394): at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394): at android.widget.ScrollView.requestLayout(ScrollView.java:1291)
E/AndroidRuntime(394): at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
E/AndroidRuntime(394): at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394): at android.widget.TextView.checkForRelayout(TextView.java:5521)
E/AndroidRuntime(394): at android.widget.TextView.setText(TextView.java:2724)
E/AndroidRuntime(394): at android.widget.TextView.setText(TextView.java:2592)
E/AndroidRuntime(394): at android.widget.TextView.setText(TextView.java:2567)
E/AndroidRuntime(394): at com.MasterZangetsu.kentrocksoc.nextEvent$eventupdate.doInBackground(nextEvent.java:81)
E/AndroidRuntime(394): at com.MasterZangetsu.kentrocksoc.nextEvent$eventupdate.doInBackground(nextEvent.java:1)
E/AndroidRuntime(394): at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(394): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
Solution
This line here
eventText.setText("" + HTML);
is causing your problem. You can't update the UI
from doInBackground()
. It has to be done in one of the other methods or sent back to a main Activity
method. Here you could do it in onPostExecute()
Read through the AyncTask Docs
There are different ways of accomplishing this but you could change your AsyncTask
to return result to onPostExecute()
public class eventupdate extends AsyncTask<String, Void, String> // change last param
then add onPostExecute()
method and change return
statement in doInBackground()
public class eventupdate extends AsyncTask<String, Void, String>
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
eventText.setText("" + result);
}
@Override
protected String doInBackground(String... url) {
// do your work
return HTML;
Answered By - codeMagic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.