-4

As the title says, I've created an ImageView in my fragment.

    private void initBanner(String imgUrl) {

        ImageView iv = new ImageView(getActivity());
        iv.setScaleType(ScaleType.FIT_XY);
        BitmapUtils bitmapUtils = new BitmapUtils(getActivity());
        bitmapUtils.display(iv, imgUrl, new BitmapLoadCallBack<View>() {

            @Override
            public void onLoadCompleted(View arg0, String arg1, Bitmap arg2,
                    BitmapDisplayConfig arg3, BitmapLoadFrom arg4) {
                Bitmap arg = arg2;
                arg2 = null;
                arg2 = new WeakReference<Bitmap>(arg).get();
                ((ImageView) arg0).setImageBitmap(arg2);
            }

            @Override
            public void onLoadFailed(View arg0, String arg1, Drawable arg2) {

            }
        });
        bannerList.add(iv);
    }

And,my activity extended to FragmentActivity. It's not always happened, sometimes or maybe some special type. I've build a bug collector for my application, so I can see what caused.Here is some error infomations:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.view.ViewConfiguration.get(ViewConfiguration.java:365)
    at android.view.View.<init>(View.java:3791)
    at android.widget.ImageView.<init>(ImageView.java:131)
    at bb.h.b(SourceFile:301)
    at bb.h.a(SourceFile:242)
    at bb.h.a(SourceFile:239)
    at bb.i.handleMessage(SourceFile:129)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5942)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

also like this:

java.lang.NullPointerException
        at miui.util.UiUtils.getMiuiUiVersion(UiUtils.java:100)
        at miui.util.UiUtils.isV5Ui(UiUtils.java:108)
        at android.view.Injector$ViewConfigurationHook.get(Injector.java:173)
        at android.view.ViewConfiguration.get(ViewConfiguration.java:336)
        at android.view.View.<init>(View.java:3453)
        at android.widget.ImageView.<init>(ImageView.java:114)
        at bb.h.b(SourceFile:301)
        at bb.h.a(SourceFile:242)
        at bb.h.a(SourceFile:239)
        at bb.i.handleMessage(SourceFile:129)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5050)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
        at dalvik.system.NativeStart.main(Native Method)

The NullPointerException both point at line 301.

ImageView iv = new ImageView(getActivity());

I've thought about this error.Maybe the activity is a backgroud process, and unfortunately been recycled.Then I resume it,fragment could not find it's attached activity.so I do this:

if (savedInstanceState != null) {
            String FRAGMENTS_TAG = "android:support:fragments";
            // remove掉保存的Fragment
            savedInstanceState.remove(FRAGMENTS_TAG);
        }

and,override onSavedInstance method of activity:

@Override
        protected void onSaveInstanceState(Bundle outState) {

        }

But that is helpless, and I'm still a newbird for android. Please tell me how to resolve this, thanks a lot.

Vigor
  • 1,706
  • 3
  • 26
  • 47
zienhern
  • 1
  • 1

1 Answers1

0

If you can put the code which you create your Fragment in the Activity may be helpful. But from this link: link, you can see that you may have the same problem of using the getActivity from your Fragment directly. Hope this will help.

    if (savedInstanceState == null){    
        adapter.addFragment(new FirstFragment());
        adapter.addFragment(new SecondFragment());
    }else{
        Integer  count  = savedInstanceState.getInt("tabsCount");
        String[] titles = savedInstanceState.getStringArray("titles");
        for (int i = 0; i < count; i++){
            adapter.addFragment(getFragment(i), titles[i]);
        }
    }

Also, it is a good practice when using fragments to check isAdded before getActivity() is called. This helps avoid a null pointer exception when the fragment is detached from the activity. For example, an activity could contain a fragment that pushes an async task. When the task is finished, the onTaskComplete listener is called.

@Override
public void onTaskComplete(List<Feed> result) {

    progress.setVisibility(View.GONE);
    progress.setIndeterminate(false);
    list.setVisibility(View.VISIBLE);

    if (isAdded()) {

        adapter = new FeedAdapter(getActivity(), R.layout.feed_item, result);
        list.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

}
Community
  • 1
  • 1
Vigor
  • 1,706
  • 3
  • 26
  • 47