Reputation: 5192
As i am new to this "Async task" concept in android,i searched a lot of posts and i implemented.In my application when opening the application i am fetching lot of images from url,
so its delaying
a lot to open.So i have an idea to have an activity when opening the application.I have tried for the following:
My activity code:
Gallery glly;
public void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.galleryview);
new RestoreDBTask().execute();
}
class RestoreDBTask extends AsyncTask <Void, Void, Nodelist>
{
private ProgressDialog dialog;
Context ctx;
public RestoreDBTask( Context ctx) {
this.ctx = ctx;
}
@Override
protected void onPreExecute()
{
dialog = ProgressDialog.show(
ctx,"Please Wait","Loading...", true);
}
@Override
protected Nodelist doInBackground(Void... params)
{
final String URLHeading = "http://m.indiatoday.in/xml/stories/sections_listing.xml";
Heading parser = new Heading();
String xmldata = parser.getXmlFromUrl(URLHeading);
Document domelement = parser.getDomElement(xmldata);
NodeList node = domelement.getElementsByTagName("item");
context.getInstance().setAppVariable("flag", "Home");
adapter=new GalleryviewAdapter(NewspapperActivity.this);
// GalleryviewAdapter adapter= new GalleryviewAdapter();
adapter.getelement("http://m.bvdjujd.in/xml/stories/jusdjhs.xml",1);
return node;
}
@Override
protected void onPostExecute(String result)
{
Element e0 = (Element) node.item(0);
txt1.setText(parser.getValue(e0, "sectionname"));
Element e1 = (Element) node.item(1);
txt2.setText(parser.getValue(e1, "sectionname"));
Element e2 = (Element) node.item(2);
txt3.setText(parser.getValue(e2, "sectionname"));
Element e3 = (Element) node.item(3);
txt4.setText(parser.getValue(e3, "sectionname"));
Element e4 = (Element) node.item(4);
txt5.setText(parser.getValue(e4, "sectionname"));
glly.setAdapter(adapter);
DisplayMetrics metrics1 = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics1);
MarginLayoutParams mlp1 = (MarginLayoutParams) glly.getLayoutParams();
mlp1.setMargins(-(metrics1.widthPixels/2+40),mlp1.topMargin,mlp1.rightMargin, mlp1.bottomMargin );
dialog.dismiss();
}
}
My logcat:
03-12 10:24:08.430: I/Process(487): Sending signal. PID: 487 SIG: 9
03-12 10:24:22.140: D/dalvikvm(511): GC freed 2643 objects / 197184 bytes in 168ms
03-12 10:24:24.800: I/MemoryCache(511): MemoryCache will use up to 6.0MB
03-12 10:24:24.810: W/dalvikvm(511): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
03-12 10:24:24.845: E/AndroidRuntime(511): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
03-12 10:24:24.932: E/AndroidRuntime(511): java.lang.RuntimeException: An error occured while executing doInBackground()
03-12 10:24:24.932: E/AndroidRuntime(511): at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-12 10:24:24.932: E/AndroidRuntime(511): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-12 10:24:24.932: E/AndroidRuntime(511): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-12 10:24:24.932: E/AndroidRuntime(511): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-12 10:24:24.932: E/AndroidRuntime(511): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-12 10:24:24.932: E/AndroidRuntime(511): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-12 10:24:24.932: E/AndroidRuntime(511): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-12 10:24:24.932: E/AndroidRuntime(511): at java.lang.Thread.run(Thread.java:1096)
03-12 10:24:24.932: E/AndroidRuntime(511): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-12 10:24:24.932: E/AndroidRuntime(511): at android.os.Handler.<init>(Handler.java:121)
03-12 10:24:24.932: E/AndroidRuntime(511): at com.newspapper.ImageLoader.<init>(ImageLoader.java:29)
03-12 10:24:24.932: E/AndroidRuntime(511): at com.newspapper.GalleryviewAdapter.<init>(GalleryviewAdapter.java:52)
03-12 10:24:24.932: E/AndroidRuntime(511): at com.newspapper.NewspapperActivity$RestoreDBTask.doInBackground(NewspapperActivity.java:569)
03-12 10:24:24.932: E/AndroidRuntime(511): at com.newspapper.NewspapperActivity$RestoreDBTask.doInBackground(NewspapperActivity.java:1)
03-12 10:24:24.932: E/AndroidRuntime(511): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-12 10:24:24.932: E/AndroidRuntime(511): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-12 10:24:24.932: E/AndroidRuntime(511): ... 4 more
03-12 10:24:25.000: I/dalvikvm(511): threadid=7: reacting to signal 3
03-12 10:24:25.214: I/dalvikvm(511): Wrote stack trace to '/data/anr/traces.txt'
Please help me to solve this.ALso help me that whether i am doing the right way..Thanks in advance..
Upvotes: 0
Views: 589
Reputation: 132982
you are getting
The method findViewById(int) is undefined for the type RestoreDBTask
because you are trying to access UI elements from RestoreDBTask class . you can solved this issue using Activity context instead of RestoreDBTask as :
TextView txt1 = (TextView)Your_Activity_Name.this.findViewById(R.id.Text01);
TextView txt2 = (TextView)Your_Activity_Name.this.findViewById(R.id.Text02);
TextView txt3 = (TextView)Your_Activity_Name.this.findViewById(R.id.Text03);
//....
but when u try to running current app you will get following error :
alledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
fto avoid this error you will need following Streps :
1. First get data from webservice inside doInBackground
.
2. Return NodeList node
from doInBackground
method
3. finally get NodeList in onPostExecute
and do your xml parsing then access or update UI elements
Example :-
class RestoreDBTask extends AsyncTask <Void, Void, NodeList>
{
///.. your code here.....
@Override
protected NodeList doInBackground(Void... params)
{
//....your code here..
return node;
}
@Override
protected void onPostExecute(NodeList result)
{
// get data from result
....
// access UI elements here add value to it
TextView txt1 = (TextView)Your_Activity_Name.
this.findViewById(R.id.Text01);
TextView txt2 = (TextView)Your_Activity_Name.
this.findViewById(R.id.Text02);
......
dialog.dismiss();
}
}
Upvotes: 2