簡體   English   中英

android片段android.support.v4.app.fragment使應用程序崩潰

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

我已經調試了幾天,不確定是什么引起了問題。 這是我的基本設置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>

注意到片段標簽? 很多示例都使用片段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)

有人可以幫忙嗎? 並足以使用上面的代碼運行它。 還是讓我知道我做錯了什么?

您應該在fragment標簽中使用android:name

更改此行...

<fragment name="com.example.BreadListFragment"

...對此...

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

可能是您在xml布局文件中錯誤地拼寫了片段類名稱。 或您的xml片段文件中有錯誤。 無論如何,請仔細檢查您的xml。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM