我无法弄清楚为什么我会收到此错误。以下是我的代码:
View.java
public class View extends FragmentActivity {
public static final String tview = "View";
Context context;
public void onCreate(Bundle savedInstanceState)
{
try
{
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.view_1);
}
catch(Exception ex)
{
Log.e(tview, ex.getMessage());
ex.printStackTrace();
}
}
ViewArrayAdapter.java
public class ViewArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private ArrayList<String> values;
public ViewArrayAdapter(Context context, ArrayList<String> values)
{
super(context, R.layout.listview, values);
this.context= context;
this.values=values;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.listview, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label12);
CheckBox chkbox = (CheckBox) rowView.findViewById(R.id.chkbox12);
textView.setText(values.get(position));
return rowView;
}
}
ViewFragment.java
public class ViewFragment extends ListFragment{
public static final String tview = "View1";
Context context;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.view_fragment,
container, false);
try
{
ArrayList<String> list = getViewList();
setListAdapter(new ViewArrayAdapter(getActivity(), list));
}catch(Exception ex)
{
Log.e(tview, ex.getMessage());
}
return view;
}
private ArrayList<String> getViewList()
{
List<Vector> list = new ArrayList<Vector>();
File root = android.os.Environment.getExternalStorageDirectory();
File sdImageMainDirectory1 = new File(root, "/java/");
String file_path = sdImageMainDirectory1.toString() + "/applicants.txt";
String entireFile = "";
try
{
BufferedReader br = new BufferedReader(new FileReader(file_path));
try {
String line = br.readLine();
while(line!=null)
{
StringTokenizer st = new StringTokenizer(line,"|");
int count=0;
String tok1="", tok2="", tok3="";
Vector<String> vec = new Vector<String>();
while(st.hasMoreTokens()) {
switch(count){
case 0:
tok1 = st.nextToken();
break;
case 1:
tok2 = st.nextToken();
break;
case 2:
tok3 = st.nextToken();
break;
default:
break;
}
count++;
}
vec.add(tok1);
vec.add(tok2);
vec.add(tok3);
list.add(vec);
line=br.readLine();
}
} catch (Exception e) {
Log.e(tview, "error while reading applicants.txt!");
}
}catch (Exception e)
{
Log.e(tview, "error while initializing applicants.txt!");
}
ArrayList<String> mylist = new ArrayList<String>();
int i=0;
while(i<list.size()){
String mystring = list.get(i).elementAt(0).toString() + " ";
mystring +=list.get(i).elementAt(1).toString() + " ";
mystring +=list.get(i).elementAt(2).toString() + " ";
mylist.add(mystring);
i++;
}
return mylist;
}
}
listview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<CheckBox
android:id="@+id/chkbox12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp">
</CheckBox>
<TextView
android:id="@+id/label12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30px" >
</TextView>
</LinearLayout>
view_1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0f0"
android:orientation="vertical">
<include layout="@layout/view" />
<fragment
android:id="@+id/view_fragment"
class="com.example.test.ViewFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
view.xml用
<?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="620dp"
android:background="#ffffffff"
android:orientation="vertical"
android:id="@+id/layout">
<TextView android:layout_width="fill_parent"
android:layout_height="70dp" android:text="TEST"
android:background="#614767"
android:textSize="32sp"
android:textColor="#ffffffff"
android:gravity="center"
android:paddingLeft="40dp"
android:id="@+id/header"
/>
<ImageView
android:layout_height="46dp"
android:src="@drawable/logo_web"
android:id="@+id/logo"
android:layout_width="70dp"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingTop="10dp"
/>
</RelativeLayout>
view_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="@+id/chkbox121"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp">
</CheckBox>
<TextView
android:id="@+id/label121"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30px" >
</TextView>
</LinearLayout>
logcat消息
04-25 01:48:40.680: E/View(7473): Binary XML file line #9: Error inflating class fragment
04-25 01:48:40.681: W/System.err(7473): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
04-25 01:48:40.682: W/System.err(7473): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
04-25 01:48:40.683: W/System.err(7473): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
04-25 01:48:40.683: W/System.err(7473): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
04-25 01:48:40.683: W/System.err(7473): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
04-25 01:48:40.683: W/System.err(7473): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
04-25 01:48:40.683: W/System.err(7473): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
04-25 01:48:40.684: W/System.err(7473): at android.app.Activity.setContentView(Activity.java:2145)
04-25 01:48:40.684: W/System.err(7473): at com.example.test.View.onCreate(View.java:56)
04-25 01:48:40.684: W/System.err(7473): at android.app.Activity.performCreate(Activity.java:5990)
04-25 01:48:40.684: W/System.err(7473): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
04-25 01:48:40.684: W/System.err(7473): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
04-25 01:48:40.685: W/System.err(7473): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
04-25 01:48:40.685: W/System.err(7473): at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-25 01:48:40.685: W/System.err(7473): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
04-25 01:48:40.685: W/System.err(7473): at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 01:48:40.685: W/System.err(7473): at android.os.Looper.loop(Looper.java:135)
04-25 01:48:40.685: W/System.err(7473): at android.app.ActivityThread.main(ActivityThread.java:5254)
04-25 01:48:40.686: W/System.err(7473): at java.lang.reflect.Method.invoke(Native Method)
04-25 01:48:40.686: W/System.err(7473): at java.lang.reflect.Method.invoke(Method.java:372)
04-25 01:48:40.686: W/System.err(7473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-25 01:48:40.686: W/System.err(7473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-25 01:48:40.686: W/System.err(7473): Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
04-25 01:48:40.688: W/System.err(7473): at android.app.ListFragment.ensureList(ListFragment.java:402)
04-25 01:48:40.688: W/System.err(7473): at android.app.ListFragment.onViewCreated(ListFragment.java:203)
04-25 01:48:40.689: W/System.err(7473): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:875)
04-25 01:48:40.689: W/System.err(7473): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
04-25 01:48:40.689: W/System.err(7473): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
04-25 01:48:40.690: W/System.err(7473): at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
04-25 01:48:40.690: W/System.err(7473): at android.app.Activity.onCreateView(Activity.java:5328)
04-25 01:48:40.690: W/System.err(7473): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
04-25 01:48:40.690: W/System.err(7473): ... 20 more
有人可以帮助我找出我在这里做错了吗?
由于
答案 0 :(得分:1)
我能解决此错误。以下是我对代码所做的更改
ViewFragment.java
public class ViewFragment extends ListFragment{
public static final String tview = "View1";
Context context;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.view_fragment, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
ArrayList<String> list = getViewList();
ViewArrayAdapter adapter = new ViewArrayAdapter(getActivity(), list);
setListAdapter(adapter);
}
}
view_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>