我的问题是,当我将此代码放入一个活动时,它工作正常,但当我尝试使用片段时,它会产生错误 这是显示错误的地方:
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<News> newItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<News> newItems) {
this.activity = activity;
this.newItems = newItems;
}
@Override
public int getCount() {
return newItems.size();
}
@Override
public Object getItem(int location) {
return newItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
// getting new data for the row
News m = newItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getImageURL(), imageLoader);
// title
title.setText(m.getTitle());
return convertView;
}
}
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
这是日志:
05-06 01:36:54.754 23005-23005 / pager.fakher.com.mypager E / AndroidRuntime:致命异常:主要 显示java.lang.NullPointerException at pager.fakher.com.mypager.adapter.CustomListAdapter。(CustomListAdapter.java:24) at pager.fakher.com.mypager.fragments.ActualiteFragment.onCreateView(ActualiteFragment.java:62) 在android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) 在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) 在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) 在android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490) 在android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1072) 在android.support.v4.view.ViewPager.populate(ViewPager.java:918) 在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1440) 在android.view.View.measure(View.java:15876) 在android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728) 在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477) 在android.view.View.measure(View.java:15876) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5023) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在android.view.View.measure(View.java:15876) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5023) 在com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302) 在android.view.View.measure(View.java:15876) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5023) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2206) 在android.view.View.measure(View.java:15876) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1929) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1013) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5507) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) 在android.view.Choreographer.doCallbacks(Choreographer.java:562) 在android.view.Choreographer.doFrame(Choreographer.java:532) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) 在android.os.Handler.handleCallback(Handler.java:730) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5473) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:525) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:854) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) 在dalvik.system.NativeStart.main(本地方法)
第24行是:
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
答案 0 :(得分:1)
我的看法是你忘了在清单中添加该应用程序实现,如下所示
<application
android:name=".package.AppController "
...
/>
这就是getInstance
返回null的原因。
答案 1 :(得分:0)
从Application类的android指南:
注意:通常不需要子类Application。在大多数情况下,静态单例可以以更模块化的方式提供相同的功能。如果您的单例需要全局上下文(例如注册广播接收器),则在调用单例的getInstance()方法时将Context.getApplicationContext()包含为Context参数。
https://developer.android.com/reference/android/app/Application.html
制作一个普通的单身人士课程并在那里接听电话。这是一个简单的例子:
public class Single {
private static Single mInstance;
private Single {}
public static Single getInstance() {
if (mInstance == null)
mInstance = new Single();
return mInstance;
}
}