android片段android.support.v4.app.fragment崩溃了应用程序

时间:2012-10-26 20:05:55

标签: android android-fragments android-support-library

我已经调试好几天了,不确定导致问题的原因。 这是我的基本设置 1.使用android 4.1 sdk 2.想要使用片段,还需要支持旧设备,所以我使用的是android.support.v4。*

这是我唯一的活动

MyActivity.java

package com.example;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

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

我的main.xml布局文件由MyActivity.java充实

<?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"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, MyActivity"
    />
    <fragment name="com.example.BreadListFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
              />
</LinearLayout>

注意片段标签? 很多例子都使用了fragment class = .... 我无法使用它,因为我使用android.support.v4.app.Fragment,它会给我一个错误,说不能将该特定片段转换为android.app.fragment(来自api级别11)

com.example.BreadListFragment.java具有以下代码

    package com.example;

import android.app.Activity;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class BreadListFragment extends ListFragment {
  int currentSelectedBreadPos = 0;

  @Override
  public void onAttach(Activity myActivity) {
    super.onAttach(myActivity);
  }

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

    if (savedInstanceState != null) {
      currentSelectedBreadPos = savedInstanceState.getInt("curBreadPos", 0);
    }
    setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, new String[] {"Banana", "Apple", "Watermelon"}));
    ListView lv = getListView();
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lv.setSelection(currentSelectedBreadPos);
  }

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

  @Override
  public void onDetach() {
    super.onDetach();
  }
}

使用上面的代码,我试图运行,应用程序崩溃,以下完整的堆栈道 但这里有什么崩溃

 10-26 13:01:36.972: INFO/ActivityManager(1903): Start proc com.example for activity com.example/.MyActivity: pid=2036 uid=10058 gids={1015, 1023}
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MyActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
        at com.example.MyActivity.onCreate(MyActivity.java:13)

完整堆栈跟踪

10-26 12:59:14.847: INFO/ActivityManager(1903): Process com.example (pid 1351) has died.
10-26 13:01:34.787: INFO/ApplicationPolicy(1903): isApplicationInstallationEnabled :  pkg com.example
10-26 13:01:34.797: INFO/PackageParser(1903): com.example: compat added android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_PHONE_STATE
10-26 13:01:34.842: INFO/PackageManager(1903): Removing non-system package:com.example
10-26 13:01:34.842: INFO/ActivityManager(1903): Force stopping package com.example uid=10058
10-26 13:01:34.932: INFO/PackageManager(1903): ICS_DEBUG scanPackageLI entered  com.example
10-26 13:01:34.932: INFO/PackageManager(1903): ICS_DEBUG checking for  com.example
10-26 13:01:34.932: INFO/PackageManager(1903): Running dexopt on: com.example
10-26 13:01:35.187: INFO/ActivityManager(1903): Force stopping package com.example uid=10058
10-26 13:01:35.327: DEBUG/PackageManager(1903): New package installed in /data/app/com.example-2.apk
10-26 13:01:35.567: INFO/ActivityManager(1903): Force stopping package com.example uid=10058
10-26 13:01:35.642: DEBUG/Launcher.LauncherModel(2152): --> package:com.example
10-26 13:01:35.662: DEBUG/Launcher.LauncherModel(2152): --> update package com.example
10-26 13:01:35.662: DEBUG/Launcher.LauncherModel(2152): --> package:com.example
10-26 13:01:35.692: INFO/SocialHub(28751): [UinboxReceiver] onReceive() >>   intent.getData() : com.example
10-26 13:01:36.897: INFO/ActivityManager(1903): START {flg=0x10000000 cmp=com.example/.MyActivity} from pid 2015
10-26 13:01:36.972: INFO/ActivityManager(1903): Start proc com.example for activity com.example/.MyActivity: pid=2036 uid=10058 gids={1015, 1023}
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MyActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
        at com.example.MyActivity.onCreate(MyActivity.java:13)
有人可以帮忙吗?并且足够用上面的代码来运行它。 或者让我知道我做错了什么?

2 个答案:

答案 0 :(得分:5)

您应该在android:name代码中使用fragment

更改此行...

<fragment name="com.example.BreadListFragment"

......对此...

<fragment android:name="com.example.BreadListFragment"

答案 1 :(得分:0)

您可能在xml布局文件中错误地拼写了您的片段类名称。或者xml片段文件中存在错误。无论如何要仔细检查你的xmls。