3
我很困惑使用ListFragment ......我無法理解問題出在哪裏?的Android ListFragment是混亂的
這裏是我FragmentActivity類:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class menu extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.root_menu);
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
ListFrag list = new ListFrag();
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, list).commit();
}}}
我ListFragment類:
public class ListFrag extends ListFragment {
int Files = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
getListView().setBackgroundResource(R.drawable.table);
getListView().setDividerHeight(0);
getListView().setCacheColorHint(0);
View view = inflater.inflate(R.layout.main, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new CustomListAdapter(getActivity(), getThemes()));
}
public String[] getThemes(){
...
return ...;
}
而且main.xml中(它是用於製作清單,自定義列表適配器我做了):
<?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="wrap_content"
android:paddingLeft="40dp"
android:paddingTop="30dp"
android:cacheColorHint="#00000000"
android:layout_marginTop="100px"
>
<ImageView
android:id="@+id/icon"
android:layout_height="40dp"
android:layout_width="40dp"
android:src="@drawable/check" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:paddingTop="6dp"
android:paddingLeft="20dp"
android:textSize="25px" >
</TextView>
</LinearLayout>
這root_menu.xml應顯示在頂部的背景圖像和列表的片段:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/back"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/back"
android:cacheColorHint="#00000000"
>
<fragment
android:id="@+id/frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.osmgames.kartuves.ListFrag" >
</fragment>
</LinearLayout>
這是我CustomListAdapter類:
public class CustomListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public CustomListAdapter(Context context, String[] values) {
super(context, R.layout.main, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
imageView.setImageResource(R.drawable.check);
return rowView;
}
}
LOG:
05-07 11:49:43.017: D/AndroidRuntime(1216): Shutting down VM
05-07 11:49:43.017: W/dalvikvm(1216): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-07 11:49:43.047: E/AndroidRuntime(1216): FATAL EXCEPTION: main
05-07 11:49:43.047: E/AndroidRuntime(1216): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.osmgames.kartuves/com.osmgames.kartuves.menu}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.os.Looper.loop(Looper.java:123)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-07 11:49:43.047: E/AndroidRuntime(1216): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 11:49:43.047: E/AndroidRuntime(1216): at java.lang.reflect.Method.invoke(Method.java:521)
05-07 11:49:43.047: E/AndroidRuntime(1216): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-07 11:49:43.047: E/AndroidRuntime(1216): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-07 11:49:43.047: E/AndroidRuntime(1216): at dalvik.system.NativeStart.main(Native Method)
05-07 11:49:43.047: E/AndroidRuntime(1216): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-07 11:49:43.047: E/AndroidRuntime(1216): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.app.Activity.setContentView(Activity.java:1647)
05-07 11:49:43.047: E/AndroidRuntime(1216): at com.osmgames.kartuves.menu.onCreate(menu.java:12)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-07 11:49:43.047: E/AndroidRuntime(1216): ... 11 more
05-07 11:49:43.047: E/AndroidRuntime(1216): Caused by: java.lang.IllegalStateException: Content view not yet created
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
05-07 11:49:43.047: E/AndroidRuntime(1216): at com.osmgames.kartuves.ListFrag.onCreateView(ListFrag.java:22)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1156)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:270)
05-07 11:49:43.047: E/AndroidRuntime(1216): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
05-07 11:49:43.047: E/AndroidRuntime(1216): ... 20 more
我看到它說contentview沒有創建,但我不明白爲什麼...
實際上有什麼問題? – Shaiful
它崩潰了...我現在要添加日誌 –
好吧看看日誌 –