gpt4 book ai didi

用于 fragment 上自定义列表的android空指针异常设置适配器

转载 作者:行者123 更新时间:2023-11-29 15:20:34 25 4
gpt4 key购买 nike

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

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

NullPointerException

这是我的代码,

用于显示左表在 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(adapter);

谢谢!

编辑 1

汉堡适配器.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

最佳答案

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

<?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
}

关于用于 fragment 上自定义列表的android空指针异常设置适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18755717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com