简体   繁体   中英

Fragment wont work

Hi i am new to android development.

I am using fragments - but i dont entirely understand how it works.

When i launch the app it crashes.

I have googled a lot but cannon solve this :(

I have an Activity(MainActivity) that must use a fragment. MainActivity looks like:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

And the main.xml contains a ListView and a Fragment:

<ListView 
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<fragment class="com.todolist.NewItemFragment"
    android:id="@+id/titles_fragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

My fragment extends ListFragment and looks like:

public class NewItemFragment extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.new_item_fragment, container, false);
    }
}

And the new_item_fragment.xml looks like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView 
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

I Dont know why it crashes. I must have misunderstood something. I hope someone out there can help - dont know what to do :)

THX


I have imported the library:

android.support.v4.app.FragmentActivity

And made the MainActivity class extend FragmentActivity

It gives me the following error:

在此处输入图片说明

Modify the new_item_fragment.xml layout file like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView 
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

to solve the most obvious error. If the app still crashes then post the Logcat with the exception.

Also, as you're extending the Activity class make sure you only use the Fragment related classes from the SDK(not the compatibility package if you have it registered in your app). This classes are available starting from Honeycomb .

If you're going to use FragmentActivity then make sure you use the Fragment class from the compatibility package(from where the FragmentActivity comes).

Try replace your fragment by follow code:

public class NewItemFragment extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.new_item_fragment, null, false);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM