【问题标题】:create listview on fragment custom drawer在片段自定义抽屉上创建列表视图
【发布时间】:2014-10-02 17:03:13
【问题描述】:

我的代码有问题, 我试图在我的片段上创建一个新的列表视图。 我正在使用自定义抽屉。

这是我的代码和我在第 53 行的错误。 查看 rootView = inflater.inflate(R.layout.fragment_layout_schedule, container, false); //这里有错误代码

这是我的完整代码。

package com.example.blackcustomzier.skripsi;

import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Blackcustomzier on 10/2/14.
 */
public class FragmentSchedule extends ListFragment {


    // 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"
    };

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("ZZZ", "ada di oncreateView");
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_layout_schedule, container, false);
        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);
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt","cur" };

        // Ids of views in listview_layout
        int[] to = {R.id.txt,R.id.cur};

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);

        setListAdapter(adapter);

        return rootView;
    }

}

这是我的 fragment_layout_schedule

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/country_fragment"
        android:name="com.example.blackcustomzier.skripsi.FragmentSchedule"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

这是目录错误

10-02 07:59:42.207    2103-2103/com.example.blackcustomzier.skripsi E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.view.InflateException: Binary XML file line #7: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.example.blackcustomzier.skripsi.FragmentSchedule.onCreateView(FragmentSchedule.java:53)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
            at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
            at android.app.Activity.onCreateView(Activity.java:4746)

感谢之前

【问题讨论】:

  • 你能发布堆栈跟踪和 fragment_layout_schedule.xml 吗?
  • @sunil 我已经附上了我的 fragment_layout_schedule.xml
  • @user2103379 第 53 行的错误.. "View rootView = inflater.inflate(R.layout.fragment_layout_schedule, container, false);"
  • 又是什么错误?是异常、空指针异常还是什么......你一定在 logcat 中看到过吧?

标签: android listview fragment


【解决方案1】:

这是因为您尝试扩展的布局不包含 ID 为“@android:id/list”的 ListView 对象。您需要在布局 xml 中包含此对象。您要做的是膨胀常规片段并将列表片段映射到此,但问题是没有支持 ListView 对象可将您的 ListFragment 映射到。

请参阅http://developer.android.com/reference/android/app/ListFragment.html 的说明,了解如何为您的列表片段创建自定义视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-24
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多