android片段android.support.v4.app.fragment使应用程序崩溃 [英] android fragment android.support.v4.app.fragment crashing the app

查看:76
本文介绍了android片段android.support.v4.app.fragment使应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经调试了好几天,不确定是什么引起了问题.这是我的基本设置1.使用Android 4.1 SDK2.想要使用片段,但也需要支持较旧的设备,因此我正在使用android.support.v4.*

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.*

这是我唯一的活动

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);
    }
}

由MyActivity.java夸大的我的main.xml布局文件

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

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

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)

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

com.example.BreadListFragment.java has the following code

    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)

全栈跟踪

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?

推荐答案

您应该在 fragment 标签中使用 android:name .

更改此行...

<fragment name="com.example.BreadListFragment"

...对此...

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

这篇关于android片段android.support.v4.app.fragment使应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆