在对dragSortListView进行膨胀时出现NullPointerException

时间:2014-11-05 04:59:53

标签: android android-layout listview

您好我正在尝试使用bauerca使用library实现的dragSortListView从服务器加载图像。我有一个简单的布局和imageview用作拖动手柄。我写了以下代码

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;
import com.squareup.picasso.Picasso;

public class MainActivity extends Activity { 
DragSortListView listView;
ReorderAdapter adapter;

private DragSortListView.DropListener onDrop = new DragSortListView.DropListener()
{
    @Override
    public void drop(int from, int to)
    {
        if (from != to)
        {
            Model item = (Model) adapter.getItem(from);
            adapter.list.remove(from);
            adapter.list.add(to, item);
        }
    }
};

private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener()
{
    @Override
    public void remove(int which)
    {
        adapter.list.remove(which);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (DragSortListView) findViewById(R.id.listview);
    String[] names = new String[]{"http://url1",
            "http://url2","http://url3", "http://url4"};

    ArrayList<Model> list = new ArrayList<Model>();
    for (int i=0; i<names.length; i++) {
        list.add(new Model(names[i], names[i], i));
    }
    adapter = new ReorderAdapter(this, R.layout.item_layout, list);
    listView.setAdapter(adapter);
    listView.setDropListener(onDrop);
    listView.setRemoveListener(onRemove);

    DragSortController controller = new DragSortController(listView);
    controller.setDragHandleId(R.id.menu);
            //controller.setClickRemoveId(R.id.);
    controller.setRemoveEnabled(false);
    controller.setSortEnabled(true);
    controller.setDragInitMode(1);
            //controller.setRemoveMode(removeMode);

    listView.setFloatViewManager(controller);
    listView.setOnTouchListener(controller);
    listView.setDragEnabled(true);
}

private class Model {
    private String text;
    private String url;
    private int position;

    public Model (String text, String url, int position) {
        this.text = text;
        this.url = url;
        this.position = position;
    }

    @Override
    public boolean equals(Object o) {
        if (((Model)o).position == this.position) 
            return true;
        return false;
    }
}
protected DragSortListView getListView() {
    // TODO Auto-generated method stub
    return (DragSortListView) this.findViewById(R.id.listview);
}
private class ReorderAdapter extends BaseAdapter {
    private ArrayList<Model> list;
    Context context;

    public ReorderAdapter(Context context, int resource, ArrayList<Model> objects) {
        this.list = objects;
    }

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

        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.item_layout, parent, false);
        }  

        ((TextView)v.findViewById(R.id.textView)).setText(list.get(position).text);
        String photo_source=list.get(position).url.toString().replaceAll(" ", "%20");
        try {
            Picasso.with(context).load(photo_source)
            .placeholder(R.drawable.ic_launcher)
            .error(R.drawable.ic_launcher)
            .into(((ImageView)v.findViewById(R.id.menu)));
        } catch(IllegalArgumentException illegalArg){
            illegalArg.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
        return v;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

但是当我尝试在NullPointerException方法中获取LayoutInflater时,我一直收到getView()

下面的

是异常的堆栈跟踪。

11-05 10:20:21.801: E/AndroidRuntime(10513): java.lang.NullPointerException
11-05 10:20:21.801: E/AndroidRuntime(10513):    at com.example.dndlist.MainActivity$ReorderAdapter.getView(MainActivity.java:120)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at com.mobeta.android.dslv.DragSortListView$AdapterWrapper.getView(DragSortListView.java:730)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.AbsListView.obtainView(AbsListView.java:2207)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.ListView.makeAndAddView(ListView.java:1845)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.ListView.fillDown(ListView.java:678)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.ListView.fillFromTop(ListView.java:739)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.ListView.layoutChildren(ListView.java:1661)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at com.mobeta.android.dslv.DragSortListView.layoutChildren(DragSortListView.java:2136)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.AbsListView.onLayout(AbsListView.java:2037)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.View.layout(View.java:14094)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:985)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.View.layout(View.java:14094)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.View.layout(View.java:14094)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.View.layout(View.java:14094)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.View.layout(View.java:14094)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.ViewGroup.layout(ViewGroup.java:4464)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1947)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.Choreographer.doFrame(Choreographer.java:548)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.os.Handler.handleCallback(Handler.java:725)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.os.Looper.loop(Looper.java:153)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at android.app.ActivityThread.main(ActivityThread.java:5297)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at java.lang.reflect.Method.invokeNative(Native Method)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at java.lang.reflect.Method.invoke(Method.java:511)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-05 10:20:21.801: E/AndroidRuntime(10513):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

更改此代码,

private class ReorderAdapter extends BaseAdapter {
    private ArrayList<Model> list;
    Context context;

    public ReorderAdapter(Context context, int resource, ArrayList<Model> objects) {
        this.list = objects;
        this.context = context;
    }

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

        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.item_layout, parent, false);
        }  

        ((TextView)v.findViewById(R.id.textView)).setText(list.get(position).text);
        String photo_source=list.get(position).url.toString().replaceAll(" ", "%20");
        try {
            Picasso.with(context).load(photo_source)
            .placeholder(R.drawable.ic_launcher)
            .error(R.drawable.ic_launcher)
            .into(((ImageView)v.findViewById(R.id.menu)));
        } catch(IllegalArgumentException illegalArg){
            illegalArg.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
        return v;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
}

答案 1 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

首先是NPE来源于你没有初始化上下文对象,你用它来扩充布局以及使用Picasso加载图像所以在构造函数中初始化上下文对象:

this.context= context;

第二件事是,无论何时使用自定义适配器,总是尝试使用ViewHolder模式。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Viewholder holder;
    if (convertView == null) {
        holder = new Viewholder();
        convertView  = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
        holder.textView = (TextView) convertView.findViewById(R.id.textView);
        holder.menu = (TextView) convertView.findViewById(R.id.menu);

        convertView.setTag(holder);
    }else{
        holder =(Viewholder) convertView.getTag();
    }


    holder.textView.setText(list.get(position).text);
    try {
          Picasso.with(context).load(list.get(position).url.toString().replaceAll(" ", "%20")).placeholder(R.drawable.ic_launcher).error(R.drawable.ic_launcher).into(holder.menu);
    } catch(IllegalArgumentException illegalArg){
         illegalArg.printStackTrace();
    } catch(Exception e){
         e.printStackTrace();
    }
   return convertView;
}

class Viewholder {
  TextView textView;
  ImageView menu;
}