我的斗智斗勇! 我想在我的活动中添加一个片段。我已经阅读了大量有关此错误的Stackoverflow文章,但我看到的解决方案(扩展FragmentActivity,导入android.support.v4等等)都没有为我修复过。
这是我看到的错误:
02-21 13:59:52.063: E/AndroidRuntime(2165): FATAL EXCEPTION: main
02-21 13:59:52.063: E/AndroidRuntime(2165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/net.myd.app.custom.MyActivity}: android.view.InflateException: Binary XML file line #57: Error inflating class Fragment
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.os.Looper.loop(Looper.java:137)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-21 13:59:52.063: E/AndroidRuntime(2165): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 13:59:52.063: E/AndroidRuntime(2165): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 13:59:52.063: E/AndroidRuntime(2165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-21 13:59:52.063: E/AndroidRuntime(2165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-21 13:59:52.063: E/AndroidRuntime(2165): at dalvik.system.NativeStart.main(Native Method)
02-21 13:59:52.063: E/AndroidRuntime(2165): Caused by: android.view.InflateException: Binary XML file line #57: Error inflating class Fragment
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-21 13:59:52.063: E/AndroidRuntime(2165): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.app.Activity.setContentView(Activity.java:1867)
02-21 13:59:52.063: E/AndroidRuntime(2165): at net.myd.app.custom.MyActivity.onCreate(MyActivity.java:46)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.app.Activity.performCreate(Activity.java:5008)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-21 13:59:52.063: E/AndroidRuntime(2165): ... 11 more
02-21 13:59:52.063: E/AndroidRuntime(2165): Caused by: java.lang.ClassNotFoundException: android.view.Fragment
02-21 13:59:52.063: E/AndroidRuntime(2165): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
02-21 13:59:52.063: E/AndroidRuntime(2165): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-21 13:59:52.063: E/AndroidRuntime(2165): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
02-21 13:59:52.063: E/AndroidRuntime(2165): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
02-21 13:59:52.063: E/AndroidRuntime(2165): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
02-21 13:59:52.063: E/AndroidRuntime(2165): ... 22 more
这是我正在使用的片段类:
package net.myd.app.custom;
import com.example.myfirstapp.R;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MyFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.my_custom_fragment, container, false);
}
}
这是我正在使用的活动:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MyActivity extends FragmentActivity
{
// ... class fields
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_custom_activity);
selectFragment();
}
public void selectFragment()
{
Fragment fragment = new MyFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.my_fragment, fragment);
fragmentTransaction.commit();
}
// etc...
}
在我的活动的xml中,这就是我引用片段的方式:
(有一篇stackoverflow文章,它建议在片段xml代码中添加完整的类路径,我试过这个,但也没有用。)
<Fragment
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
如果有人能看到导致此错误的任何问题,我会非常感激!
由于
答案 0 :(得分:1)
替换:
<Fragment
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
有类似的东西:
<FrameLayout
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
答案 1 :(得分:0)
使用FrameLayout可以解决问题。
<FrameLayout
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" />