setAdapter中的“调试当前指令指针”

时间:2014-01-08 20:56:54

标签: android android-listview

调试我的应用时遇到问题。我尝试创建一个ListView,当setAdapter然后停止。不要在LogCat上显示错误或警告,只能停止应用。这是我的代码:

private ListView List;
private TypedArray Icons;
private ArrayList<MapListItem> mapItems;
private MapListAdapter adapter;

Icons = getResources().obtainTypedArray(R.array.icon_map);
        List = (ListView) this.getActivity().findViewById(R.id.ListMap);
        mapItems = new ArrayList<MapListItem>();
        mapItems.add(new MapListItem(Icons.getResourceId(0, -1)));
        mapItems.add(new MapListItem(Icons.getResourceId(1, -1)));
        mapItems.add(new MapListItem(Icons.getResourceId(2, -1)));
        mapItems.add(new MapListItem(Icons.getResourceId(3, -1)));
        mapItems.add(new MapListItem(Icons.getResourceId(4, -1)));
        Icons.recycle();
        adapter = new MapListAdapter(getActivity().getApplicationContext(), mapItems);
        List.setAdapter(adapter); //This is pointer error

MapListAdapter.java

public class MapListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<MapListItem> Items;

    public MapListAdapter(Context context, ArrayList<MapListItem> Items){
        this.context = context;
        this.Items = Items;
    }

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

    @Override
    public Object getItem(int position) {       
        return Items.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_map, null);
        }
        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon_map);
        imgIcon.setImageResource(Items.get(position).getIcon());        

        return convertView;
    }

}

MapListItem.java

public class MapListItem {
    private int icon;

    public MapListItem(){}

    public MapListItem(int icon){
        this.icon = icon;
    }
    public int getIcon(){
        return this.icon;
    }   
    public void setIcon(int icon){
        this.icon = icon;
    }
}

fragment.xml(ListView在哪里)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="fill_vertical" >

    <ListView
        android:id="@+id/ListMap"
        android:layout_width="50dp"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:divider="#fff"
        android:dividerHeight="1dp"
        android:background="#16a085" >
    </ListView>

</RelativeLayout>

list_map.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:padding="5dp"
    android:background="@null">
    <ImageView 
        android:id="@+id/icon_map"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:background="@null"
        android:layout_centerVertical="true"
        android:contentDescription="TODO"/>
</RelativeLayout>

fragment.java

public class fragmentMap extends Fragment {
    ...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment, container, false);
        return rootView;
    }
...
}

为什么要破坏应用程序?

1 个答案:

答案 0 :(得分:0)

您拥有的代码正在片段中运行。你在哪里:

List = (ListView) this.getActivity().findViewById(R.id.ListMap);

引用您在父Activity中充气的视图层次结构,而不是您在片段中充气的视图层次结构。在Fragment中,您将fragment.xml扩展为变量'rootView':

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

因此,您应该在片段代码中的rootView上调用findViewById:

List = (ListView) rootView.findViewById(R.id.ListMap);