简体   繁体   English

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

[英]android fragment android.support.v4.app.fragment crashing the app

i have been debugging for days, not sure what is causing the issue. 我已经调试了几天,不确定是什么引起了问题。 here is my basic setup 1. using android 4.1 sdk 2. want to use fragment, but also need to support older devices, so i am using android.support.v4.* 这是我的基本设置1.使用android 4.1 sdk 2.要使用片段,但还需要支持较旧的设备,所以我正在使用android.support.v4。*

here is my only activity 这是我唯一的活动

MyActivity.java 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);
    }
}

my main.xml layout file inflated by MyActivity.java 我的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>

notice the fragment tag? 注意到片段标签? a lot of example uses fragment class=.... i could not use that because i am using the android.support.v4.app.Fragment, it will give me an error saying cannot covert that particular fragment to android.app.fragment (which is from api level 11) 很多示例都使用片段class = ....我无法使用它,因为我使用的是android.support.v4.app.Fragment,它会给我一个错误,提示无法将特定片段隐藏到android.app.fragment (来自api级别11)

com.example.BreadListFragment.java has the following code 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();
  }
}

with the above code, i tried to run, the app crashes, with the following full stack tract but here is what is crashing 使用上面的代码,我试图运行,应用程序崩溃,并出现以下完整堆栈,但这是崩溃的原因

 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)

full stack trace 全栈跟踪

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)

can someone eplease help? 有人可以帮忙吗? and kind enough to run it with code above. 并足以使用上面的代码运行它。 or let me know what i am doing wrong? 还是让我知道我做错了什么?

You should be using android:name in your fragment tag. 您应该在fragment标签中使用android:name

Change this line... 更改此行...

<fragment name="com.example.BreadListFragment"

... to this... ...对此...

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

Probably you have incorrectly spelled your fragment class name in your xml layout file. 可能是您在xml布局文件中错误地拼写了片段类名称。 Or there is an error in your xml fragment file. 或您的xml片段文件中有错误。 Anyway double check your xmls. 无论如何,请仔细检查您的xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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