3

アプリを ICS 用に準備するために、フラグメントに頭を悩ませようとしています。

最も基本的なフラグメント アプリを取得するための次のファイルがあります。起動時にこれが必要です: テキスト ビュー "Fragment 1" を持つ 1 つのフラグメント レイアウトと、その隣に "Fragment2" を持つ別のフラグメント レイアウト。

私のパッケージ名はcom.mwerner.fragments

私のファイルは次のとおりです。

  • FragmentsActivity.java
  • 例Fragment.java
  • 例Fragment2.java
  • examples_fragment.xml
  • examples_fragment2.xml
  • main.xml

FragmentsActivity.java のコードは次のとおりです。

package com.mwerner.fragments;

import android.app.Activity;
import android.os.Bundle;

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

例Fragment.java

package com.mwerner.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExamplesFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.examples_fragment, container, false);
    }
}

例Fragment2.java

package com.mwerner.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExamplesFragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.examples_fragment2, container, false);
    }
}

examples_fragment.xml ファイルには、テキストビューを含む線形レイアウトが含まれているだけです... main.xml のコードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
        class="com.mwerner.fragments$ExamplesFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
         />

<fragment 
        class="com.mwerner.fragments$ExamplesFragment2"
        android:id="@+id/viewer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="fill_parent" />


</LinearLayout>

アプリが起動時にエラーでクラッシュする

11-07 18:12:12.519: E/AndroidRuntime(696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mwerner.fragments/com.mwerner.fragments.FragmentsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

ここで何が間違っているのか教えてください。フラグメントのGoogle開発者ページからコードをほとんどコピー/貼り付けました。

4

3 に答える 3

5

レイアウト xml でフラグメントへのパスを正しく定義していません。クラス属性を修正してください。これを試して:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
        class="com.mwerner.fragments.ExamplesFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
         />

<fragment 
        class="com.mwerner.fragments.ExamplesFragment2"
        android:id="@+id/viewer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="fill_parent" />


</LinearLayout>
于 2011-11-08T16:56:04.750 に答える
0

Fragments を使用して最初のプログラムを作成していたときに、同じエラーが発生しました。"Activity" を拡張する代わりに、ランチャー アクティビティで "FragmentActivity" を拡張します。

于 2012-07-10T11:20:39.327 に答える
0

FragmentActivity を拡張してみる

  public class FragmentsActivity extends FragmentActivity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }
于 2011-12-13T17:25:07.690 に答える