为了让我的应用程序为 ICS 做好准备,我正试图了解 Fragments。
我有以下文件来获取您可以拥有的最基本的片段应用程序。启动时应该有这个:一个带有文本视图“Fragment 1”的片段布局,旁边是另一个带有“Fragment2”的片段布局。
我的包裹名称是com.mwerner.fragments
我的文件是:
- 片段活动.java
- 示例Fragment.java
- 示例Fragment2.java
- 示例_fragment.xml
- 示例_fragment2.xml
- 主要的.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
你能告诉我这里有什么问题吗?我几乎从谷歌开发者页面复制/粘贴了代码片段。