GridAdapter抛出空引用错误

时间:2015-12-20 07:11:55

标签: android

我有三个片段类,每个都有自己的自定义适配器(我使用这三个片段和ViewPager。)

//Fragment class 1
static MyListAdapterArtists myListAdapterArtists;

//Fragment class 2
static MyListAdapterTracks myListAdapterTracks;

//Fragment class 3
static MyGridViewAdapter myGridViewAdapter;

所有三个适配器都基于相同的基础数据。我想在同一个班级中的任何一个上打notifyDataSetChanged()。所以我将它们传递给UpdateAdapters班。

//Fragment class 1
UpdateAdapters.getInstance().setAdapterOne(adapter);

//Fragment class 2
UpdateAdapters.getInstance().setAdapterTwo(adapter);

//Fagment class 3
UpdateAdapters.getInstance().setAdapterThree(gridAdapter);

这是UpdateAdapters

public class UpdateAdapters {

private static UpdateAdapters instance = null;

private MyListAdapterArtists myListAdapterArtists;
private MyListAdapterTracks myListAdapterTracks;
private MyGridViewAdapter myGridViewAdapter;

//Constructor
protected UpdateAdapters() { }

//Get the current instance
public static UpdateAdapters getInstance() {
    if (instance == null) {
        // create a new one if it doesn't exist
        instance = new UpdateAdapters();
    }
    return instance;
}

//Setters
public void setAdapterOne(MyListAdapterTracks myListAdapterTracks){
    this.myListAdapterTracks = myListAdapterTracks;
}

public void setAdapterTwo(MyListAdapterArtists myListAdapterArtists){
    this.myListAdapterArtists = myListAdapterArtists;
}

public void setAdapterThree(MyGridViewAdapter myGridViewAdapter){
    this.myGridViewAdapter = myGridViewAdapter;
}

//Public methods
public void update(){

    if(null != myListAdapterArtists) {
        myListAdapterArtists.notifyDataSetChanged();
        Log.v("TAG", "myListAdapterArtists successfully updated ");
    } else {
        Log.v("TAG","myListAdapterArtists is a null reference ");}

    if(null != myListAdapterTracks) {
        myListAdapterTracks.notifyDataSetChanged();
        Log.v("TAG", "myListAdapterTracks successfully updated ");
    } else {
        Log.v("TAG","myListAdapterTracks is a null reference ");}

    if(null != myGridViewAdapter) {
        myGridViewAdapter.notifyDataSetChanged();
        Log.v("TAG", "myGridViewAdapter successfully updated ");
    } else {
        Log.v("TAG","myGridViewAdapter is a null reference ");
    }
}

我可以单独更新任何适配器

UpdateAdapters.getInstance().update();

我的问题是只有myListAdapterTracksmyListAdapterArtists正在更新。 myGridViewAdapter抛出空引用错误。

知道问题可能是什么?这有足够的信息吗?

由于

修改

这是我的堆栈跟踪

E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.myname.myapp, PID: 30271
      java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.myname.myapp.MyGridViewAdapter.notifyDataSetChanged()' on a null object reference
          at com.example.myname.myapp.UpdateAdapters.update(UpdateAdapters.java:54)
          at com.example.myname.myapp.ItunesApiClass$2$1$1$1.run(ItunesApiClass.java:164)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:145)
          at android.app.ActivityThread.main(ActivityThread.java:5944)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

修改

这是我的网格视图适配器类

public class MyGridViewAdapter extends ArrayAdapter<AlbumObject> {


    Context context;
    int layoutResourceId;
    ArrayList<AlbumObject> albumObjectList;

    public MyGridViewAdapter(Context context, int layoutResourceId, ArrayList<AlbumObject> albumObjectList) {

        super(context, layoutResourceId, albumObjectList);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.albumObjectList = albumObjectList;
    }

    static class ViewHolder {

        static ImageView albumArt;
        static TextView albumTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        AlbumObject albumObject = albumObjectList.get(position);

        ViewHolder viewHolder; // view lookup cache stored in tag

        // if an existing view is not being reused
        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());

            convertView = inflater.inflate(R.layout.item_grid_view, parent, false);
            convertView.setTag(viewHolder);

            // if an existing view is being reused
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.albumTitle = (TextView) convertView.findViewById(R.id.gridViewAlbumTitle);
        viewHolder.albumArt = (ImageView) convertView.findViewById(R.id.gridViewAlbumArt);

        viewHolder.albumTitle.setText(albumObject.albumTitle);

        if(albumObject.albumArtURI != null){

            File f = new File(albumObject.albumArtURI);
            Picasso.with(viewHolder.albumArt.getContext())
                    .load(f)
                    .transform(new CircleTransform())
                    .placeholder(R.drawable.blackcircle).fit().centerCrop()
                    .into(viewHolder.albumArt);
        } else {

            Picasso.with(viewHolder.albumArt.getContext())
                    .load(albumObject.albumArtURI)
                    //.transform(new CircleTransform())
                    .placeholder(R.drawable.blackcircle)
                    .into(viewHolder.albumArt);
        }

        return convertView;
    }
}

这是网格视图的片段类

public class MyFragmentAlbums extends Fragment {

    /** Fragment Variables **/
    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
    static GridView gridView;
    static ArrayList<AlbumObject> albumObjectList;

    /** Static Factory Method for Fragment Instantiation **/
    public static final MyFragmentAlbums newInstance(ArrayList<AlbumObject> arrayList)
    {
        MyFragmentAlbums f = new MyFragmentAlbums();
        Bundle bdl = new Bundle(1);
        bdl.putParcelableArrayList(EXTRA_MESSAGE, arrayList);
        f.setArguments(bdl);
        return f;
    }

    /** On Create Method**/
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        albumObjectList = getArguments().getParcelableArrayList(EXTRA_MESSAGE);

        View rootView = inflater.inflate(R.layout.fragment_layout_grid, container, false);

        gridView = (GridView) rootView.findViewById(R.id.gridView);

        MyGridViewAdapter gridAdapter = new MyGridViewAdapter(getActivity(), R.layout.item_grid_view, albumObjectList);
        gridView.setAdapter(gridAdapter);

        UpdateAdapters.getInstance().setAdapterThree(gridAdapter);

        return rootView;
    }
}

1 个答案:

答案 0 :(得分:0)

问题是网格适配器是页面适配器中的第三个片段。除非我滑过一个,否则它为空。所以我一直在更新一个尚未退出的适配器。解决了这个问题。

viewPager.setOffscreenPageLimit(3);
viewPager.setAdapter(pageAdapter);