2

我已经调试了几天,不知道是什么导致了这个问题。这是我的基本设置 1. 使用 android 4.1 sdk 2. 想使用片段,但也需要支持旧设备,所以我使用的是 android.support.v4.*

这是我唯一的活动

我的活动.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>

注意片段标签?很多示例使用片段类=....我无法使用它,因为我使用的是 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)

有人可以帮忙吗?并且友好地使用上面的代码运行它。或者让我知道我做错了什么?

4

2 回答 2

5

你应该android:name在你的fragment标签中使用。

改变这一行...

<fragment name="com.example.BreadListFragment"

……到这……

<fragment android:name="com.example.BreadListFragment"
于 2012-10-26T20:40:31.340 回答
0

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

于 2012-10-26T20:20:34.610 回答