setAdapter(适配器)始终出错

时间:2014-10-03 02:37:24

标签: android listview fragment

我想创建一个列表视图 但我不明白的事情,我的listview总是错误 有人可以帮我解决这个问题吗?

这是我的代码

public class FragmentSchedule extends Fragment {

    ListView list;
    // Array of strings storing country names
    String[] countries = new String[] {
            "India",
            "Pakistan",
            "Sri Lanka",
            "China",
            "Bangladesh",
            "Nepal",
            "Afghanistan",
            "North Korea",
            "South Korea",
            "Japan"
    };

    // Array of strings to store currencies
    String[] currency = new String[]{
            "Indian Rupee",
            "Pakistani Rupee",
            "Sri Lankan Rupee",
            "Renminbi",
            "Bangladeshi Taka",
            "Nepalese Rupee",
            "Afghani",
            "North Korean Won",
            "South Korean Won",
            "Japanese Yen"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("ZZZ", "ada di oncreateView maintain");
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_layout_schedule, container, false);
// Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

        for(int i=0;i<10;i++) {
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("txt", "Country : " + countries[i]);
            hm.put("cur", "Currency : " + currency[i]);
            aList.add(hm);
            // R.layout.listview_layout defines the layout of each item
            list = (ListView) getActivity().findViewById(R.id.list);
            ListAdapter adapter = new SimpleAdapter(getActivity(), aList, R.layout.list_v_s, new String[]{"txt", "cur"}, new int[]{R.id.txt, R.id.cur});

            list.setAdapter(adapter);

        }
        return rootView;
    }
}

这是我的fragment_layout_schedule

<?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:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="455dp"
        android:layout_above="@+id/getdata" />

</LinearLayout>

这是我的list_v_s

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

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/cur"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp" />

</LinearLayout>

这是我的logcat

10-02 22:31:50.562    2548-2548/com.example.blackcustomzier.skripsi E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.blackcustomzier.skripsi.FragmentSchedule.onCreateView(FragmentSchedule.java:69)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
            at android.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
            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:5103)
            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:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

三江源

2 个答案:

答案 0 :(得分:0)

R.id.list ListView位于fragment_layout_schedule,因此请使用rootView初始化ListView而不是getActivity()

        list = (ListView) rootView.findViewById(R.id.list);

答案 1 :(得分:0)

替换

 list = (ListView) getActivity().findViewById(R.id.list);

对齐
list = (ListView)rootView.findViewById(R.id.list);