我尝试在片段内使用通用图像加载器,但出现空指针异常。原始流程如下所示:
Activity A extends AbsListViewBaseActivity
{
public class ImageAdapter extends BaseAdapter {
//this flow is working, images are showing
imageLoader.displayImage(values2.get(position).get(TAG_IMAGE_NAME2 ).toString(), holder.image, options2);
}
}
AbsListViewBaseActivity extends BaseActivity
{
public ImageLoader imageLoader = ImageLoader.getInstance();
}
使用 Fragments,我想复制上面的实现,但我在 imageLoader 中得到了空指针。我唯一不能做的就是扩展 AbsListViewBaseActivity 并扩展 BaseActivity(初始化图像加载器的那个),因为下面扩展了Fragment
public class CollectionDemoActivity extends FragmentActivity {
static ImageAdapter imageAptr ;
public BaseActivity base;
public static class DemoObjectFragment extends Fragment {
((GridView) absList.listView).setAdapter(imageAptr);
}
public class ImageAdapter extends BaseAdapter {
//Getting Errors in here/ null pointer
base.imageLoader.displayImage("Image Path", holder.image, options2);
}
}
日志猫:
07-18 18:49:36.073: E/AndroidRuntime(20970): FATAL EXCEPTION: main
07-18 18:49:36.073: E/AndroidRuntime(20970): java.lang.NullPointerException
07-18 18:49:36.073: E/AndroidRuntime(20970): at com.jinisys.restoplusordering.CollectionDemoActivity$ImageAdapter.getView(CollectionDemoActivity.java:403)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.widget.AbsListView.obtainView(AbsListView.java:2267)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.View.measure(View.java:15181)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.View.measure(View.java:15181)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1447)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.View.measure(View.java:15181)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.View.measure(View.java:15181)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.View.measure(View.java:15181)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-18 18:49:36.073: E/AndroidRuntime(20970): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.View.measure(View.java:15181)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.Choreographer.doFrame(Choreographer.java:525)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.os.Handler.handleCallback(Handler.java:615)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.os.Looper.loop(Looper.java:137)
07-18 18:49:36.073: E/AndroidRuntime(20970): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-18 18:49:36.073: E/AndroidRuntime(20970): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 18:49:36.073: E/AndroidRuntime(20970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-18 18:49:36.073: E/AndroidRuntime(20970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-18 18:49:36.073: E/AndroidRuntime(20970): at dalvik.system.NativeStart.main(Native Method)