1

我有一个片段工作正常的项目,我使用 actionbarsherlock 和 jeremyfeinstein 的滑动菜单,

一切正常,但是当我想自定义左侧抽屉列表单元格时,出现异常

空指针异常

这是我的代码,

用于显示左表在 list 的基本模式下正常工作,simple_list_item_1

但是当我自定义它时,我得到了例外,

这里是列表 [PhoneMenuList.java]:

 public class PhoneMenuList extends SherlockListFragment{

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    return super.onCreateView(inflater, container, savedInstanceState);  

}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    //SET THE LIST ADAPTER!

    Hamburger hambu_data[] = new Hamburger[]
            {
                new Hamburger(R.drawable.icon_hambu_folder, "My Documents"),
                new Hamburger(R.drawable.icon_hambu_favs, "Top 10 viewed"),
                new Hamburger(R.drawable.icon_hambu_validate, "Validate"),
                new Hamburger(R.drawable.icon_hambu_how, "How to use"),
                new Hamburger(R.drawable.icon_hambu_how, "About")
            };              




        ListView productList= (ListView) getActivity().findViewById(R.id.listView1);

      //for context!
        HamburgerAdapter adapter = new HamburgerAdapter(getActivity(), R.layout.hamburger_item_row, hambu_data);            


        /*
        View header = (View)getLayoutInflater(savedInstanceState).inflate(R.layout.hamburger_item_row, null);
        productList.addHeaderView(header);


        productList.setAdapter(adapter);

        */       
} 
 }

在 list.xml 上

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/listView1"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#000000"
    android:dividerHeight="1dip" />

汉堡包项目行

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

     <ImageView android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

     <TextView android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

</LinearLayout>

那么当我设置我的适配器时如何解决异常

productList.setAdapter(适配器);

谢谢!

编辑 1

HamburgerAdapter.java

public class HamburgerAdapter extends ArrayAdapter<Hamburger>{

    Context context; 
    int layoutResourceId;    
    Hamburger data[] = null;


    public HamburgerAdapter(Context context, int layoutResourceId, Hamburger[] data) {
        super(context, layoutResourceId, data);

        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            //inflater for fragment!

            LayoutInflater inflater = ((Activity)context).getLayoutInflater();



            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Hamburger hamburger = data[position];
        holder.txtTitle.setText(hamburger.title);
        holder.imgIcon.setImageResource(hamburger.icon);

        return row;
    }

    static class WeatherHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }

}

编辑 2,

例外,使用时:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

   // return super.onCreateView(inflater, container, savedInstanceState);  

    View mView = inflater.inflate(R.layout.list, container, false);
    return mView; 
}

01-01 11:43:27.110: W/dalvikvm(14502): threadid=1: thread exiting with uncaught exception (group=0x410292a0)
01-01 11:43:27.110: E/AndroidRuntime(14502): FATAL EXCEPTION: main
01-01 11:43:27.110: E/AndroidRuntime(14502): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chocolate.tiks_documents/com.chocolate.tiks_documents.MainActivityPhone}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.os.Looper.loop(Looper.java:137)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.app.ActivityThread.main(ActivityThread.java:4898)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at java.lang.reflect.Method.invokeNative(Native Method)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at java.lang.reflect.Method.invoke(Method.java:511)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at dalvik.system.NativeStart.main(Native Method)
01-01 11:43:27.110: E/AndroidRuntime(14502): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1167)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.app.Activity.performStart(Activity.java:5216)
01-01 11:43:27.110: E/AndroidRuntime(14502):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
01-01 11:43:27.110: E/AndroidRuntime(14502):    ... 11 more
4

2 回答 2

3

您忘记为列表项设置 ID。您的适配器还需要处理有关其包含的数据的委托信息。覆盖getCount()可用于确定您拥有多少项目以及基本适配器应尝试创建多少视图。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hamburger_item_row"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

     <ImageView android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

     <TextView android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

</LinearLayout>

对于适配器,您需要添加以下内容

@Override
public int getCount() {
    return data.size();    //Base adapter needs to now how many items to create
}
于 2013-09-12T05:11:20.467 回答
2

尝试使用以下代码

public class PhoneMenuList extends SherlockFragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View mView = inflater.inflate(R.layout.list, container, false);
    return mView;  

}
于 2013-09-12T05:15:49.107 回答