从横向切换到纵向会产生NullPointerException

时间:2013-11-29 11:48:59

标签: android nullpointerexception

我正在创建一个在列表中显示事件的应用。每个事件都有一个自定义布局(使用BaseAdapter)。如果我只在纵向使用我的手机或仅使用横向风景,但如果我在列表中并旋转手机以更改方向,则BaseAdapter会生成NullPointerException。这是代码:

public class ShowListAdapter extends BaseAdapter {
    private Activity a;
    private List<Map<String, String>> monthList;
    private static LayoutInflater inflater;

    public ShowListAdapter(Activity act, List<Map<String, String>> msc) {
        a = act;
        monthList = msc;
        inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return monthList.size();
    }

    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;

        if (view == null) {
            v = inflater.inflate(R.layout.list_item, null);
        }

        /* Some random code that formats the view goes here */

        return v;
    }
}

错误在getCount()抛出,因为它(我假设)找不到列表变量monthList

此类用于ListFragment类

public class ListForMonth extends ListFragment {
    List<Map<String, String>> eventListLoc;

    public ListForMonth() {}

    public ListForMonth(int i) {
        eventListLoc = ShowHolder.eventList.get(i);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setListAdapter(new ShowListAdapter(this.getActivity(), eventListLoc));
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

ShowHolder类有一个静态变量,它在应用程序的初始屏幕中“生成”。

Activity有多个标签,每个标签都有自己的ListFragment。我想,代码中唯一重要的部分就是这个:

public Fragment getItem(int position) {
    return new ListForMonth(position);
}

为什么代码会抛出此错误?我该如何解决?提前谢谢!

2 个答案:

答案 0 :(得分:2)

你是对的问题就在这一行......

public Fragment getItem(int position) {
return new ListForMonth(position);
}

当方向发生变化时,Android会使用默认构造函数调用fragmnet ..那时候List&gt; eventListLoc数据为null ..这就是为什么它给NPE ..

要避免这种情况。请更改像这样的片段代码..

public class ListForMonth extends ListFragment {
List<Map<String, String>> eventListLoc;
private int i;

public ListForMonth() {
}

public ListForMonth(int i) {
    this.i = i;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        i = savedInstanceState.getInt("POSITION");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    eventListLoc = ShowHolder.eventList.get(i);
    setListAdapter(new ShowListAdapter(this.getActivity(), eventListLoc));
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("POSITION", i);
}
}

答案 1 :(得分:0)

请注意,因为当您从横向切换到纵向或从纵向切换到横向时,活动会重新启动。因此,覆盖onResume()方法并处理屏幕方向。

相关问题