使用ListView在片段中的.setAdapter上的NullPointerException

时间:2014-04-29 20:48:55

标签: java android

我有一个片段活动,并且在.setAdapter调用中有一个nullpointerexception,当我输入片段时我的app力会关闭。我真的不明白为什么。该片段是标签界面的一部分。

这是我的代码:

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import edu.purdue.ugl.adapters.BaseInflaterAdapter;
import edu.purdue.ugl.adapters.CardItemDataMultiple;
import edu.purdue.ugl.adapters.inflaters.CardInflaterMultiple;

public class StickyBoard extends Fragment {
    Button addButton;
    ListView listview;
    BaseInflaterAdapter<CardItemDataMultiple> adapter;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.activity_sticky_board,container,false);

        addButton = (Button) rootView.findViewById(R.id.newStickyBoard);
        listview = (ListView) rootView.findViewById(R.id.list_view);
        adapter=new BaseInflaterAdapter<CardItemDataMultiple>(new CardInflaterMultiple());
        listview.setAdapter(adapter);
        return rootView;
    }
}

BaseInflaterAdapter.java

package edu.purdue.ugl.adapters;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public class BaseInflaterAdapter<T> extends BaseAdapter
{
    private List<T> m_items = new ArrayList<T>();
    private IAdapterViewInflater<T> m_viewInflater;

    public BaseInflaterAdapter(IAdapterViewInflater<T> viewInflater)
    {
        m_viewInflater = viewInflater;
    }

    public BaseInflaterAdapter(List<T> items, IAdapterViewInflater<T> viewInflater)
    {
        m_items.addAll(items);
        m_viewInflater = viewInflater;
    }

    public void setViewInflater(IAdapterViewInflater<T> viewInflater, boolean notifyChange)
    {
        m_viewInflater = viewInflater;

        if (notifyChange)
            notifyDataSetChanged();
    }

    public void addItem(T item, boolean notifyChange)
    {
        m_items.add(item);

        if (notifyChange)
            notifyDataSetChanged();
    }

    public void addItems(List<T> items, boolean notifyChange)
    {
        m_items.addAll(items);

        if (notifyChange)
            notifyDataSetChanged();
    }

    public void clear(boolean notifyChange)
    {
        m_items.clear();

        if (notifyChange)
            notifyDataSetChanged();
    }

    @Override
    public int getCount()
    {
        return m_items.size();
    }

    @Override
    public Object getItem(int pos)
    {
        return getTItem(pos);
    }

    public T getTItem(int pos)
    {
        return m_items.get(pos);
    }

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

    @Override
    public View getView(int pos, View convertView, ViewGroup parent)
    {
        return m_viewInflater != null ? m_viewInflater.inflate(this, pos, convertView, parent) : null;
    }
}

错误:

04-29 19:47:23.051  28727-28727/com.ugl.ugl E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: edu.purdue.ugl, PID: 28727
    java.lang.NullPointerException
            at edu.purdue.ugl.StickyBoard.onCreateView(StickyBoard.java:33)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2301)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1950)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1129)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1311)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1016)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5711)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5184)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1015)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
            at dalvik.system.NativeStart.main(Native Method)

不确定如何解决此问题。

1 个答案:

答案 0 :(得分:2)

我真的不确定,但是你应该尝试附加ContextFragment是父活动:

adapter = new BaseInflaterAdapter<CardItemDataMultiple>(getActivity(), new CardInflaterMultiple());

然后,通过以下内容接收此上下文:

public class BaseInflaterAdapter<T> extends BaseAdapter {
    private Context m_context;
    private List<T> m_items = new ArrayList<T>();
    private IAdapterViewInflater<T> m_viewInflater;

    public BaseInflaterAdapter(Context context, IAdapterViewInflater<T> viewInflater) {
        m_context = context;
        m_viewInflater = viewInflater;
    }

    public BaseInflaterAdapter(Context context, List<T> items, IAdapterViewInflater<T> viewInflater) {
        m_context = context;
        m_items.addAll(items);
        m_viewInflater = viewInflater;
    }

    // other stuff...

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        return m_viewInflater != null ? m_viewInflater.inflate(m_context, pos, convertView, parent) : null;
    }

}