My problem is that when i put this code in an activity, it works fine, but when i am trying to use fragments, it's generating errors this is where the error shown:
我的问题是,当我把这个代码放在一个活动中时,它运行正常,但是当我尝试使用片段时,它会产生错误,这就是显示的错误:
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);
}
}
}
and this is the log:
这是日志:
05-06 01:36:54.754 23005-23005/pager.fakher.com.mypager E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at pager.fakher.com.mypager.adapter.CustomListAdapter.(CustomListAdapter.java:24) at pager.fakher.com.mypager.fragments.ActualiteFragment.onCreateView(ActualiteFragment.java:62) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) at android.support.v4.view.ViewPager.populate(ViewPager.java:1072) at android.support.v4.view.ViewPager.populate(ViewPager.java:918) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1440) at android.view.View.measure(View.java:15876) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477) at android.view.View.measure(View.java:15876) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5023) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:15876) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5023) at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302) at android.view.View.measure(View.java:15876) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5023) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2206) at android.view.View.measure(View.java:15876) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1929) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1013) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5507) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) at android.view.Choreographer.doCallbacks(Choreographer.java:562) at android.view.Choreographer.doFrame(Choreographer.java:532) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5473) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) at dalvik.system.NativeStart.main(Native Method)
05-06 01:36:54.754 23005-23005 / pager.fakher.com.mypager E / AndroidRuntime:FATAL EXCEPTION:pager.fakher.com.mypager.adapter.CustomListAdapter中的主要java.lang.NullPointerException。(CustomListAdapter.java: 24)在android.support.v4.app的android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)的pager.fakher.com.mypager.fragments.ActualiteFragment.onCreateView(ActualiteFragment.java:62) .FalmmentManagerImpl.moveToState(FragmentManager.java:955)位于android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)的android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)at在android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)android.support.v4.app.FragmentPagerAdapter.finishUpdate的android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) FragmentPagerAdapter.java:141)在android.support.v4.view.Vie上的android.support.v4.view.ViewPager.populate(ViewPager.java:1072)在Android.widget.RelativeLayout的android.view.View.measure(View.java:15876)的android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1440)上的wPager.populate(ViewPager.java:918) .measureChildHorizontal(HorizontalLayout.java:728)位于android.view.measure.View.measure(View.java:15876)的android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477),位于android.view.ViewGroup.measureChildWithMargins(ViewGroup。 java:5023)在android.view.measure.View.measure(View.java:15876)的android.widget.FrameLayout.onMeasure(FrameLayout.java:310)android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5023)at at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)at android.view.View.measure(View.java:15876)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5023)at android .widget.FrameLayout.onMeasure(FrameLayout.java:310)位于android.view.View.measure(View.java)的com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2206) :15876)在Android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)的android.view.ViewRootImpl.performMevers(ViewRootImpl.java:1913)android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293) .view.ViewRootImpl.doTraversal(ViewRootImpl.java:1013)在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5507)android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)at android。在Android.view.Handler的android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)的android.view.Choreographer.doFrame(Choreographer.java:532)上查看.Choreographer.doCallbacks(Choreographer.java:562) .handleCallback(Handler.java:730)在android.app.Handler.dispatchMessage(Handler.java:92)android.app.Looper.loop(Looper.java:137)android.app.ActivityThread.main(ActivityThread。 java:5473)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:525)at com.android.internal.os.ZygoteIni t $ MethodAndArgsCaller.run(ZygoteInit.java:854)位于dalvik.system.NativeStart.main(本地方法)的com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
the line 24 is :
第24行是:
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
2 个解决方案
#1
My take is you forgot to add that application implementation in the manifest like this
我的看法是你忘了在清单中添加这样的应用程序实现
<application
android:name=".package.AppController "
...
/>
That's why getInstance
returns null.
这就是getInstance返回null的原因。
#2
From the android guides of the Application class:
从Application类的android指南:
Note: There is normally no need to subclass Application. In most situations, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), include Context.getApplicationContext() as a Context argument when invoking your singleton's getInstance() method.
注意:通常不需要子类Application。在大多数情况下,静态单例可以以更模块化的方式提供相同的功能。如果您的单例需要全局上下文(例如注册广播接收器),则在调用单例的getInstance()方法时将Context.getApplicationContext()包含为Context参数。
https://developer.android.com/reference/android/app/Application.html
Make a regular singleton class and get the calls there. Here's a simple example:
制作一个普通的单身人士课程并在那里接听电话。这是一个简单的例子:
public class Single {
private static Single mInstance;
private Single {}
public static Single getInstance() {
if (mInstance == null)
mInstance = new Single();
return mInstance;
}
}
#1
My take is you forgot to add that application implementation in the manifest like this
我的看法是你忘了在清单中添加这样的应用程序实现
<application
android:name=".package.AppController "
...
/>
That's why getInstance
returns null.
这就是getInstance返回null的原因。
#2
From the android guides of the Application class:
从Application类的android指南:
Note: There is normally no need to subclass Application. In most situations, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), include Context.getApplicationContext() as a Context argument when invoking your singleton's getInstance() method.
注意:通常不需要子类Application。在大多数情况下,静态单例可以以更模块化的方式提供相同的功能。如果您的单例需要全局上下文(例如注册广播接收器),则在调用单例的getInstance()方法时将Context.getApplicationContext()包含为Context参数。
https://developer.android.com/reference/android/app/Application.html
Make a regular singleton class and get the calls there. Here's a simple example:
制作一个普通的单身人士课程并在那里接听电话。这是一个简单的例子:
public class Single {
private static Single mInstance;
private Single {}
public static Single getInstance() {
if (mInstance == null)
mInstance = new Single();
return mInstance;
}
}