开发者

Listfragment Crashes pretty basic application every time

开发者 https://www.devze.com 2023-04-01 01:49 出处:网络
I am currently trying to work with Android ActionBar Sherlock on my Nexus One running Gingerbread 2.3.4.

I am currently trying to work with Android ActionBar Sherlock on my Nexus One running Gingerbread 2.3.4. I try to develop a FragmentList which enables me to create a Menu in this activity. The menu should now consist out of "item1 - item4" However sometime later I want the menu to consists out of a custom made list.

However, whenever I try to execute my App it force closes for some reason or another.

This is the layout file:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="0dip"
android:background="@drawable/bg">
<TextView 
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hauptmenü"
android:background="@drawable/custom_bg_title"
android:padding="5dip"
android:gravity="center"
android:textStyle="bold"
android:height="30px"
 />

<fragment class="com.w..TitlesFragment"
        android:id="@+id/titles" android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />


</LinearLayout>

This is the codefile of the activity

package com.;

import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
import android.support.v4.view.SubMenu;
import android.support.v4.view.Window;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class wcw_realActivity extends FragmentActivity implements OnItemClickListener {


private static final String[] items ={"item1","item2","item3","item4"};
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    SubMenu subMenu1 = menu.addSubMenu("Action Item");
        subMenu1.add("Sample");
        subMenu1.add("Menu");
        subMenu1.add("Items");

    MenuItem subMenu1Item = subMenu1.getItem();
        subMenu1Item.setIcon(R.drawable.ic_title_share_de开发者_高级运维fault);
        subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);          

    return super.onCreateOptionsMenu(menu);
}

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

    requestWindowFeature(Window.FEATURE_ACTION_BAR);        
    setContentView(R.layout.main);                       
}   

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Log.i("FragmentList", "Item clicked: "+ id);
}

public static class TitlesFragment extends ListFragment {
    boolean mDualPane;
    int mCurCheckPosition = 0;

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

        // Populate list with our static array of titles.
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,
                items));

        if (savedState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedState.getInt("curChoice", 0);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v, int pos, long id) {
        Log.i("FragmentList", "Item clicked: "+ id);
    }

}

}

I suspect that I initialize the fragment on a bad way - which would probably explain that the App crashes every time.

When I exclude the "fragment" Tag out of my app it works just fine.

It would be great if someone knows a solution for that particular problem.

Here is the logfile


ListFragment (or any other Fragment) is not available in Gingerbread by default. You might be compiling against Honeycomb which is why it compiles but crashes when run. Use the compatibility package to use Fragments in older versions of Android. Also, make sure you have your minSdkVersion set to the right one.

If you're already using the compatibility package, maybe you are not including it correctly (since the ListFragment class cannot be found)?

0

精彩评论

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

关注公众号