私はAndroidでこの「非同期タスク」の概念に慣れていないので、多くの投稿を検索して実装しました.私のアプリケーションでは、開くことがたくさんあるwhen opening the application i am fetching lot of images from url,
のでdelaying
、アプリケーションを開くときにアクティビティを作成するという考えがあります.次のことを試しました:
私の活動コード:
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();
}
}
私の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'
これを解決するのを手伝ってください。また、私が正しい方法を行っているかどうかを助けてください..前もって感謝します..