我开始学习Android中的Fragment范例,并决定修改一个使用它的简单应用程序。我的应用程序显示项目列表和纵向模式 - 它完美地工作 - 它转向横向模式显示应该在右侧显示前面的列表和文本框,这里出现问题。请原谅我的很多代码:
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="home.etudes.News"
android:id="@+id/list_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:name="home.etudes.News"
android:id="@+id/list_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
<fragment
android:name="home.etudes.ExtendedNews"
android:id="@+id/extended_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
</LinearLayout>
入口点
public class FrgamentEtudeActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
列表片段
public class News extends ListFragment {
final String[] array = new String[] {"Article one", "Article two", "Article three"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(
getActivity().getApplicationContext(),
R.array.tut_titles, R.layout.list_item));
}
}
在启动横向模式并且应该调用下面的代码之前,
前面的代码工作正常(实际上它没有,我已经把断点但应用程序包含在另一部分中):
public class ExtendedNews extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_item, container);
return view;//textView;
}
}
如果对问题有什么想法,请与我分享。
这里是来自logcat的信息:
05-02 17:52:05.345:E / AndroidRuntime(325):致命异常:主要 05-02 17:52:05.345:E / AndroidRuntime(325): java.lang.RuntimeException:无法启动活动ComponentInfo {home.etudes / home.etudes.FrgamentEtudeActivity}:android.view.InflateException:Binary XML文件行#15:错误扩充类片段 05-02 17:52:05.345:E / AndroidRuntime(325):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.app.ActivityThread.access $ 1600(ActivityThread.java:117) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.os.Handler.dispatchMessage(Handler.java:99) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.os.Looper.loop(Looper.java:123) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.app.ActivityThread.main(ActivityThread.java:3683) 05-02 17:52:05.345:E / AndroidRuntime(325):at java.lang.reflect.Method.invokeNative(Native Method) 05-02 17:52:05.345:E / AndroidRuntime(325):at java.lang.reflect.Method.invoke(Method.java:507) 05-02 17:52:05.345:E / AndroidRuntime(325):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 05-02 17:52:05.345:E / AndroidRuntime(325):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-02 17:52:05.345:E / AndroidRuntime(325):at dalvik.system.NativeStart.main(Native Method) 05-02 17:52:05.345:E / AndroidRuntime(325):引起:android.view.InflateException:二进制XML文件行#15:错误膨胀类片段 05-02 17:52:05.345:E / AndroidRuntime(325):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.view.LayoutInflater.inflate(LayoutInflater.java:408) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.view.LayoutInflater.inflate(LayoutInflater.java:320) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.view.LayoutInflater.inflate(LayoutInflater.java:276) 05-02 17:52:05.345:E / AndroidRuntime(325):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.app.Activity.setContentView(Activity.java:1657) 05-02 17:52:05.345:E / AndroidRuntime(325):at home.etudes.FrgamentEtudeActivity.onCreate(FrgamentEtudeActivity.java:13) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 05-02 17:52:05.345:E / AndroidRuntime(325):... 12更多 05-02 17:52:05.345:E / AndroidRuntime(325):引起:android.support.v4.app.Fragment $ InstantiationException:无法实例化片段home.etudes.ExtendedNews:确保类名存在,是公共的,并且有一个公开的空构造函数 05-02 17:52:05.345:E / AndroidRuntime(325):在android.support.v4.app.Fragment.instantiate(Fragment.java:391) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.support.v4.app.Fragment.instantiate(Fragment.java:359) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:262) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 05-02 17:52:05.345:E / AndroidRuntime(325):... 21更多 05-02 17:52:05.345:E / AndroidRuntime(325):引起:java.lang.ClassNotFoundException:home dalvik.system.PathClassLoader中的home.etudes.ExtendedNews [/data/app/home.etudes-2.apk ] 05-02 17:52:05.345:E / AndroidRuntime(325):at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 05-02 17:52:05.345:E / AndroidRuntime(325):at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 05-02 17:52:05.345:E / AndroidRuntime(325):at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 05-02 17:52:05.345:E / AndroidRuntime(325):在android.support.v4.app.Fragment.instantiate(Fragment.java:381)