2012-11-11 42 views
0

我正在學習如何使用ListFragment使用getview時,當它運行下面的代碼的應用程序崩潰崩潰:應用listfragment

public class profileListFragment extends ListFragment { 

String[] countries = new String[] {"USA", "China"}; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), 
      android.R.layout.simple_list_item_1,countries); 
    setListAdapter(adapter); 
    View retView = super.onCreateView(inflater, container, savedInstanceState); 

    return retView;//super.onCreateView(inflater, container, savedInstanceState); 
} 

public void onListItemClick(ListView I, View v, int position, long id){ 
    I.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    I.setSelector(android.R.color.holo_blue_light); 
} 
} 

我知道這是因爲,當getListView()語句我刪除它,它不會崩潰...

任何幫助非常讚賞

I/ActivityManager(352): START {cmp=myapp.app/.loadProfile u=0} from pid 24295 

E/AndroidRuntime(24295): java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp.app/myapp.app.loadProfile}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment 

E/AndroidRuntime(24295): at myapp.app.loadProfile.onCreate(loadProfile.java:12) 

E/AndroidRuntime(24295): at myapp.app.profileListFragment.onCreateView(profileListFragment.java:19) 

11-10 22:15:01.002: W/ActivityManager(352): Force finishing activity myapp.app/.loadProfile 
+1

發佈崩潰的logcat跟蹤。 – Squonk

+0

hm ...我下載了catlog,但不知道如何獲得logcat ...另外,我應該提到我在平板電腦上運行它不在模擬器上,不知道是否重要... – evan54

+0

您是否真的閱讀catlog的說明?或嘗試菜單選項?您可以將整個logcat輸出發送給您自己,然後在此處複製/粘貼(儘管請僅複製/粘貼與崩潰相關的部分)。或者,使用USB連接平板電腦並使用DDMS。 – Squonk

回答

0

我做的,看起來像這樣的XML:

<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/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:choiceMode="singleChoice" > 

</ListView> 

</LinearLayout> 

,然後它充氣,並做了findViewById該視圖上尋找android.R.id.list,然後有我需要擺在首位的ListView我處理。代碼片段是這樣的:

View retView = inflater.inflate(R.layout.fragment_load_profile_list_layout,container, false); 
    ListView lv = (ListView) retView.findViewById(android.R.id.list); 
    lv.setSelector(android.R.color.holo_blue_light); 
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);