我有ViewPager和带片段的适配器。
它工作得非常好,但是当我点击“主页按钮”并且20分钟后回到我的应用程序时,应用程序崩溃了“nullpointexception”。
这是我的代码:
主:
pager = (ViewPager)findViewById(R.id.pager);
pager.setOffscreenPageLimit(1); getFragments();
pageAdapter = new AdapterArticles(getSupportFragmentManager(), fragments);
pager.setAdapter(pageAdapter);
pager.setCurrentItem(extras.getInt("position");
pager.setOnPageChangeListener(this);
片段:
public static final FragmentArticle newInstance(RssItem item){
FragmentArticle f = new FragmentArticle();
Bundle bdl = new Bundle(1);
bdl.putParcelable("item", item);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View convert = inflater.inflate(R.layout.view_event, container, false);
TextView title = (TextView)convert.findViewById(R.id.title);
TextView info = (TextView)convert.findViewById(R.id.info);
Item item = getArguments().getParcelable("item");
info.setText(item.getItemDate());
title.setText(item.getTitle());
return convert;
}
堆栈:
02-04 10:05:53.545: E/AndroidRuntime(19517): FATAL EXCEPTION: main
02-04 10:05:53.545: E/AndroidRuntime(19517): java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.wm.ga/pl.wm.scroll.ActivityArticlesScroll}: java.lang.NullPointerException
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.app.ActivityThread.access$600(ActivityThread.java:139)
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.os.Looper.loop(Looper.java:154)
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.app.ActivityThread.main(ActivityThread.java:4945)
02-04 10:05:53.545: E/AndroidRuntime(19517): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 10:05:53.545: E/AndroidRuntime(19517): at java.lang.reflect.Method.invoke(Method.java:511)
02-04 10:05:53.545: E/AndroidRuntime(19517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-04 10:05:53.545: E/AndroidRuntime(19517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-04 10:05:53.545: E/AndroidRuntime(19517): at dalvik.system.NativeStart.main(Native Method)
02-04 10:05:53.545: E/AndroidRuntime(19517): Caused by: java.lang.NullPointerException
02-04 10:05:53.545: E/AndroidRuntime(19517): at pl.wm.scroll.ActivityArticlesScroll.getFragments(ActivityArticlesScroll.java:132)
02-04 10:05:53.545: E/AndroidRuntime(19517): at pl.wm.scroll.ActivityArticlesScroll.onCreate(ActivityArticlesScroll.java:69)
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.app.Activity.performCreate(Activity.java:4531)
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
02-04 10:05:53.545: E/AndroidRuntime(19517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
02-04 10:05:53.545: E/AndroidRuntime(19517): ... 11 more
我在这段代码中检查是否有问题: 我有数据的类,但是当活动恢复并且我使用getFeed时,它是null。
public class ArticlesManager {
private static ArticlesManager instance = null;
private HashMap<String, RssFeed> feedMap = new HashMap<String, RssFeed>();
private ArticlesManager() {
}
public static ArticlesManager getInstance() {
if(instance == null) {
instance = new ArticlesManager();
}
return instance;
}
public RssFeed getFeed(String name) {
RssFeed rssFeed = feedMap.get(name);
return rssFeed;
}
public void addFeed(String name, RssFeed rssFeed){
feedMap.put(name, rssFeed);
}
public void clear(){
feedMap.clear();
}
public void clearArticle(String name){
feedMap.remove(name);
}
}
答案 0 :(得分:0)
我认为您的额外内容可能为null或者getInt(“position”)可能为null。发布完整的堆栈跟踪,以便我们了解正在发生的事情。此外,您应该提供您的活动或片段的完整代码,您将获得这些额外内容。