【发布时间】: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 中看到过吧?