自定义列表视图适配器提供nullpointerexception

时间:2014-05-06 10:24:45

标签: android android-listview android-arrayadapter

我想设置一个显示列表视图的片段。我试过这样的话:

MainActivity扩展了FragmentActivity

在onCreate中:

   FragmentTransaction ft = getFragmentManager().beginTransaction();
        Inbox inbox = Inbox.newInstance();
        ft.replace(R.id.container, inbox);
        ft.commit();

在我的收件箱片段中:

public static Inbox newInstance() {
    Inbox fragmentDemo = new Inbox();
    return fragmentDemo;
}

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

    View view = inflater.inflate(R.layout.fragment_inbox, container, false);

    smsAdapter = new ListAdapter(getActivity(), smsList);
    ListView lv = (ListView) view.findViewById(R.id.list);
    lv.setAdapter(smsAdapter);

    return view;
}

我的自定义ListAdapter:

public class ListAdapter extends ArrayAdapter<SMSData> {

private String TAG = ListAdapter.class.getName();

private final Context context;
private final List<SMSData> smsList;

public ListAdapter(Context context, List<SMSData> smsList) {
    super(context, R.layout.sms_inbox, smsList);
    this.context = context;
    this.smsList = smsList;
}

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

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.sms_inbox, parent, true);

        holder = new ViewHolder();
        holder.senderNumber = (TextView) convertView.findViewById(R.id.smsNumberText);

        convertView.setTag(holder);
    }
    else
        holder = (ViewHolder) convertView.getTag();

    holder.senderNumber.setText(smsList.get(position).getNumber());

    return convertView;
}

private static class ViewHolder
{
    public TextView senderNumber;
}
}

活动xml:

<FrameLayout
    android:id="@+id/container"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
</FrameLayout>

片段xml:

  <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

Sms_inbox.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent" >
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/smsNumberText"/>
    </TableRow>
</TableLayout>

给我:

05-06 06:13:40.528    2756-2756/com.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.test, PID: 2756
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
            at android.widget.ListView.setAdapter(ListView.java:480)
            at Fragments.Inbox.onCreateView(Inbox.java:51)
            at android.app.Fragment.performCreateView(Fragment.java:1700)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.BackStackRecord.run(BackStackRecord.java:684)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
            at android.app.Activity.performStart(Activity.java:5240)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

有人可以帮我解决这个问题吗?我在网上看过很多教程,但我不知道自己在做什么。

2 个答案:

答案 0 :(得分:2)

ListAdapter添加方法:

@Override
public int getCount() {
    return smsList != null ? smsList.size() : 0;
}

答案 1 :(得分:0)

首先:所有内容都为null,因为我在设置适配器后填写了列表。

第二:在自定义适配器中,我需要将inflate中的布尔值设置为false,这样它就不会耦合到父

第三:在xml更改id / list到android:id / list