开发者

Problem using ListFragment on Android

I'm very new at programming in Android (I have programmed extensively PC applications and used Java) and I'm at a loss. I want to create a application that uses fragments. I want to create the Fragment Programaticaly (as the introduction says on Android Developer page) buy my application keeps crashing and I have no Idea as to why.

Here is the code for my main activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    FragmentManager fmanager = getFragmentManager();
    FragmentTransaction ftransaction = fmanager.beginTransaction();
    FileBrowser fbrowser = new FileBrowser();
    ftransaction.add(R.layout.main,fbrowser, "FileBrowser");       
    ftransaction.commit();

    fbrowser.test();
}

And here is the code for FileBrowser:

public class FileBrowser extends ListFragment {

public void test(){
    System.err.println("Entre a test!!!");
    String[] MyList = {"HOla","Mundo","De","Las","Listas"};
    if (getActivity() == null){
        System.err.println("I get a NULL Activity");
    }
    else{
        System.err.println("No NULL Activity Attempting to Create an Adapter");
        ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(), R.layout.file_row, MyList);
    }
    //setListAdapter(new ArrayAdapter<String>(this.getActivity(), R.layout.file_row, MyList));
}

}

All I wan to do to start is to print the string list MyList in the fragment. However I the program crashes and I get this error:

06-09 19:25:42.920: ERROR/AndroidRuntime(26064): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ccr/com.ccr.Main}: java.lang.IllegalArgumentException: No view found for id 0x7f030001 for fragment FileBrowser{4077d558 #0 id=0x7f030001 FileBrowser}

Thanks for any help!

PD: I get the I get a NULL Activity Message also.


I've tried that and it did not work either.

This is my new xml file

 <?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">
<FrameLayout 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/flFileBrowser">
    </FrameLayout>        
</LinearLayout>

And this is what my code reads:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    FragmentManager fmanager = getFragmentManager();
    FragmentTransaction ftransaction = fmanager.beginTransaction();
    FileBrowser fbrowser = new FileBrowser();
    System.err.println("I'm about to add the fragment");
    ftransaction.add(R.id.flFileBrowser,fbrowser, "FileBrowser");
    System.err.println("fragment added");
    ftransaction.commit();
    System.err.println("Done");
}

This was my output:

06-10 12:23:21.520: WARN/System.err(30826开发者_开发知识库): I'm about to add the fragment
06-10 12:23:21.520: WARN/System.err(30826): fragment added
06-10 12:23:21.520: WARN/System.err(30826): Done
06-10 12:23:21.520: WARN/dalvikvm(30826): threadid=1: thread exiting with uncaught exception (group=0x40131760)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826): FATAL EXCEPTION: main
06-10 12:23:21.520: ERROR/AndroidRuntime(30826): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ccr/com.ccr.Main}: java.lang.IllegalArgumentException: No view found for id 0x7f050000 for fragment FileBrowser{4077d3f0 #0 id=0x7f050000 FileBrowser}
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at android.os.Looper.loop(Looper.java:132)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at android.app.ActivityThread.main(ActivityThread.java:4025)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at java.lang.reflect.Method.invoke(Method.java:491)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826):     at dalvik.system.NativeStart.main(Native Method)
06-10 12:23:21.520: ERROR/AndroidRuntime(30826): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f050000 for fragment FileBrowser{4077d3f0 #0 id=0x7f050000 FileBrowser}


The problem is here:

ftransaction.add(R.layout.main,fbrowser, "FileBrowser");

The first parameter is expecting an id of a view present in the layout of the activity. You are passsing a layout. So for example let's say you have a FrameLayout in the layout of the activity with id="@+id/container" then you would call:

ftransaction.add(R.id.container,fbrowser, "FileBrowser");

http://developer.android.com/reference/android/app/FragmentTransaction.html#add%28int,%20android.app.Fragment,%20java.lang.String%29

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜