Listfragment 每次都会使基本应用程序崩溃
我目前正在尝试在运行 Gingerbread 2.3.4 的 Nexus One 上使用 Android ActionBar Sherlock。 我尝试开发一个 FragmentList,它使我能够在此活动中创建一个菜单。 菜单现在应该由“item1 - item4”组成,但是稍后我希望菜单由自定义列表组成。
但是,每当我尝试执行我的应用程序时,它都会因某种原因强制关闭。
这是布局文件:
<?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>
这是活动的代码文件
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_default);
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 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_default);
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ListFragment (或任何其他
Fragment
)是默认情况下在 Gingerbread 中不可用。您可能正在针对 Honeycomb 进行编译,这就是它可以编译但运行时崩溃的原因。使用兼容包在旧版本的Android中使用Fragment。另外,请确保您的minSdkVersion
设置正确。如果您已经在使用兼容性包,也许您没有正确包含它(因为找不到
ListFragment
类)?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 yourminSdkVersion
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)?