Android: App crashes when initializing new AsyncTask (VerifyError)

I have an an app that crashes at the Point: "new Downloader(this, mURL);":

    Downloader downloader = new Downloader(this, mURL);
    downloader.execute(0);

Downloader.java:

public class Downloader extends AsyncTask<Integer,Integer,String>{

private Fragment fragment;
private String mURL;
private Activity activity;

public Downloader(VertretungsplanFragment fr, String mURL)
{
    fragment = fr;
    this.mURL = mURL;
    this.activity = fr.getActivity();
}
@Override
protected String doInBackground(Integer... integers) {
[...]}
}

Then this Error appears (important: it only crashes on Android < 3)

java.lang.VerifyError

Here's the Error Log :)

--startDownload:253 is exactly Downloader downlaoder = new Downlaoder(this, mURL);

02-25 15:40:55.405 619-619/thems.vertretungsplan E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.VerifyError: thems.vertretungsplan.Downloader at thems.vertretungsplan.VertretungsplanFragment.startDownload(VertretungsplanFragment.java:253) at thems.vertretungsplan.VertretungsplanFragment.onCreateView(VertretungsplanFragment.java:58) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) at android.support.v4.view.ViewPager.populate(ViewPager.java:1068 ) at android.support.v4.view.ViewPager.populate(ViewPager.java:914) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) at android.view.View.measure(View.java:8313) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017) at android.widget.LinearLayout.measureVertical(LinearLayout.java:386) at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) at android.view.View.measure(View.java:8313) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) at android.view.View.measure(View.java:8313) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365) at android.view.View.measure(View.java:8313) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) at android.widget.FrameLayout.onMeasure(FrameL ayout.java:250) at android.view.View.measure(View.java:8313) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) at android.view.View.measure(View.java:8313) at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651) at android.view.View.measure(View.java:8313) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) at android.view.View.measure(View.java:8313) at android.widget.LinearLayout.measureVertical(LinearLayout.java:531) at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) at android.view.View.measure(View.java:8313) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) at android.view.View.measure(View.java:8313) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) at android.widget.FrameLayout.onMea sure(FrameLayout.java:250) at android.view.View.measure(View.java:8313) at android.view.ViewRoot.performTraversals(ViewRoot.java:839) at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3683) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method)


You have to get the context from the Activity that instanced the fragment.

context =new Context();
context = getActivity();

Pass this "context" as context to the asynctask constructor.


Fragments were introduced after android 3.0 ie API level 11

http://developer.android.com/reference/android/app/Fragment.html

链接地址: http://www.djcxy.com/p/84722.html

上一篇: ListView + ArrayList + AsyncTask

下一篇: Android:初始化新的AsyncTask(VerifyError)时,应用程序崩溃