2012-11-29 60 views
0

我稱之爲片段能夠使用。 但我不會成爲List片段。 爲什麼不能直接調用 沒有什麼錯如何使用onclickListener調用ListFragment

這是呼叫類型

teamp.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
    FragmentManager AS = getFragmentManager(); 
    FragmentTransaction USA = AS.beginTransaction(); 
//planA 
    ListFragment UF =(ListFragment) AS.findFragmentById(R.id.frameLayout2); 
//planB  
//Fragment UF = AS.findFragmentById(R.id.frameLayout2); 
//planA and planB Two ways to type over will not work 
        if (UF == null) { 
         String title = "Fragment A"; 
         templistview usermune = new templistview(title); 
         USA.add(R.id.frameLayout2, usermune); 
         USA.addToBackStack(null); 
         USA.commit(); 
         } 

這是代碼片段控制 templistview.java

public class templistview extends ListFragment { 
    String title1; 
    TextView textView; 
     private ListView listView; 
     private View v; 

    public templistview() { 

    } 

    public templistview(String title) { 
     this.title1 = title; 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     super.onCreateView(inflater, container, savedInstanceState); 
     View view = inflater.inflate(R.layout.templist, container, false); 
     listView = (ListView)view.findViewById(R.id.listView); 
     String[] arr = new String[]{ 
      "A","B","C","D","E","F","G" 
     }; 
     ArrayAdapter<String> adapter = 
      new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1,arr); 
     listView.setAdapter(adapter); 
     return view; 

    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 


    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
    } 

    @Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 

    public static Fragment findFragmentById(int framelayout) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 

這是錯誤日誌

11-29 11:29:30.208: E/AndroidRuntime(22541): FATAL EXCEPTION: main 
11-29 11:29:30.208: E/AndroidRuntime(22541): java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.app.ListFragment.ensureList(ListFragment.java:402) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.app.ListFragment.onViewCreated(ListFragment.java:203) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:842) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.app.BackStackRecord.run(BackStackRecord.java:622) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.os.Handler.handleCallback(Handler.java:605) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.os.Looper.loop(Looper.java:137) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at android.app.ActivityThread.main(ActivityThread.java:4511) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at java.lang.reflect.Method.invokeNative(Native Method) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at java.lang.reflect.Method.invoke(Method.java:511) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 
11-29 11:29:30.208: E/AndroidRuntime(22541): at dalvik.system.NativeStart.main(Native Method) 

UI接口代碼 templist.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/listView" 
    /> 
</LinearLayout> 

我已經做了兩個位置更新 但還是錯誤

11-29 11:55:18.583: E/AndroidRuntime(25053): FATAL EXCEPTION: main 
11-29 11:55:18.583: E/AndroidRuntime(25053): java.lang.NullPointerException 
11-29 11:55:18.583: E/AndroidRuntime(25053): at com.example.tr.templistview.onCreateView(templistview.java:43) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at android.app.BackStackRecord.run(BackStackRecord.java:622) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at android.os.Handler.handleCallback(Handler.java:605) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at android.os.Looper.loop(Looper.java:137) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at android.app.ActivityThread.main(ActivityThread.java:4511) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at java.lang.reflect.Method.invokeNative(Native Method) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at java.lang.reflect.Method.invoke(Method.java:511) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 
11-29 11:55:18.583: E/AndroidRuntime(25053): at dalvik.system.NativeStart.main(Native Method) 

回答

1

XML格式的列表視圖切換到

<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@android:id/list" // == here is the change 
/> 

以及在活動初始​​化列表視圖as

​​
+0

錯誤仍然存​​在我需要添加該信息? –