-1

好的,我正在尝试这样做:

一个有两个选项卡和一个选项卡的应用程序应该有一个 listView

例子:

http://picu.site11.com/images/91866108b08a600a1a7400bb1ed63a9ed82dc2a5.png

我的问题:我为适配器创建了 ListView 和 TextView。但是每次我在 ListView 中调用 setAdapter 时,应用程序都会超时

好的,第一个选项卡有这个 xml 文件(view1.xml):

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"/>
</LinearLayout>

它的片段类:

public class Chats extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.view1, container, false);
        ListView listView = (ListView) view.findViewById(R.id.listView);

        String[] arr = {"A", "B", "C", "D", "E", "F"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.row, R.id.listView, arr);
        //listView.setAdapter(adapter);
        //Log.d("MY_LOGS", "listView == null: " + (listView == null));

        return view;
    }
}

文件 row.xml 位于 TextView

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                        android:layout_width="fill_parent"
                                                        android:layout_height="?android:attr/listPreferredItemHeight"
                                                        android:padding="6dip" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewText"
        />


</RelativeLayout>

如果我用 NullPointerException 异常注释掉 listView.setAdapter 应用程序超时..

ERROR/AndroidRuntime(18321): FATAL EXCEPTION: main
        java.lang.NullPointerException
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsListView.obtainView(AbsListView.java:2624)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1254)
        at android.widget.ListView.onMeasure(ListView.java:1166)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
        at android.view.View.measure(View.java:16831)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2186)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1349)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1532)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6350)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:561)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)

可能是什么问题?没有什么是空的。。

4

1 回答 1

2

使用正确的 id:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.row, R.id.viewText, arr);
于 2013-11-02T12:21:15.830 回答