Android - 使用片段扩展另一个类

时间:2013-07-18 11:13:53

标签: java android android-fragments universal-image-loader

我试图在片段中使用通用图像加载器,但我得到Null Pointer Exception。原始流程如下所示:

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(初始化imageloader的人),因为下面扩展了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);
}

}

logcat的:

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)

1 个答案:

答案 0 :(得分:2)

需要在Imageloader类中配置

BaseAdapter,因此我在上面的代码中所做的工作非常混乱。我直接在我的主要活动BaseActivity.imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));上使用此功能,而不是依赖于AbsListViewBaseActivity类中的配置。