如何使用onclickListener调用ListFragment

时间:2012-11-29 03:44:56

标签: android fragment onclicklistener

我调用片段是可以使用的。 但我不会成为List片段。 为什么不能直接打电话 没错呢

这是电话的类型

    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 个答案:

答案 0 :(得分:1)

将xml中的listview更改为

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

并将活动中的列表视图初始化为

listView = (ListView)view.findViewById(android.R.id.list);