我在Android应用程序中有以下代码:
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="activity" />
<fragment
android:id="@+id/list"
android:name="com.hugo.test.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
fragmenta.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="helloA" />
</LinearLayout>
FragmentA.java:
package com.hugo.test;
//some imports here
public class FragmentA extends Fragment {
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
return inflater.inflate(R.layout.fragmenta, container, false);
}
}
FragmentActivity.java:
package com.hugo.test;
//some imports here
public class FragmentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
活动的布局应显示“活动”,片段应显示“helloA”。当我运行我的项目时,我只看到“活动”,所以在我看来片段被忽略了。
我看到的错误是:
12-19 09:01:05.370: E/AndroidRuntime(1352): FATAL EXCEPTION: main
12-19 09:01:05.370: E/AndroidRuntime(1352): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hugo.test/com.hugo.test.FragmentActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.os.Looper.loop(Looper.java:132)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.ActivityThread.main(ActivityThread.java:4028)
12-19 09:01:05.370: E/AndroidRuntime(1352): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 09:01:05.370: E/AndroidRuntime(1352): at java.lang.reflect.Method.invoke(Method.java:491)
12-19 09:01:05.370: E/AndroidRuntime(1352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
12-19 09:01:05.370: E/AndroidRuntime(1352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
12-19 09:01:05.370: E/AndroidRuntime(1352): at dalvik.system.NativeStart.main(Native Method)
12-19 09:01:05.370: E/AndroidRuntime(1352): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
12-19 09:01:05.370: E/AndroidRuntime(1352): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:245)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.Activity.setContentView(Activity.java:1780)
12-19 09:01:05.370: E/AndroidRuntime(1352): at com.hugo.test.FragmentActivity.onCreate(FragmentActivity.java:11)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
12-19 09:01:05.370: E/AndroidRuntime(1352): ... 11 more
12-19 09:01:05.370: E/AndroidRuntime(1352): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.hugo.test.fragmenta: make sure class name exists, is public, and has an empty constructor that is public
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.Fragment.instantiate(Fragment.java:500)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.Fragment.instantiate(Fragment.java:468)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.Activity.onCreateView(Activity.java:4132)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
12-19 09:01:05.370: E/AndroidRuntime(1352): ... 20 more
12-19 09:01:05.370: E/AndroidRuntime(1352): Caused by: java.lang.ClassNotFoundException: com.hugo.test.fragmenta in loader dalvik.system.PathClassLoader[/data/app/com.jochen.test-1.apk]
12-19 09:01:05.370: E/AndroidRuntime(1352): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
12-19 09:01:05.370: E/AndroidRuntime(1352): at java.lang.ClassLoader.loadClass(ClassLoader.java:540)
12-19 09:01:05.370: E/AndroidRuntime(1352): at java.lang.ClassLoader.loadClass(ClassLoader.java:500)
12-19 09:01:05.370: E/AndroidRuntime(1352): at android.app.Fragment.instantiate(Fragment.java:490)
12-19 09:01:05.370: E/AndroidRuntime(1352): ... 23 more
由于该错误,我向FragmentA.java添加了一个构造函数
public FragmentA()
{
}
此错误未消失,片段仍未显示,与以前相同的行为。有人有想法吗?
答案 0 :(得分:8)
这是由于0dp我从Android页面复制该部分时没有看到。
答案 1 :(得分:1)
此
public class FragmentActivity extends Activity {
应该是
public class FragmentActivity extends FragmentActivity {
这也在here
进行了讨论答案 2 :(得分:1)
使用这个,它对我有用
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View returnView = inflater.inflate(R.layout.fragmenta, container, false);
return returnView;
}
}