遗憾的是,应用程序在实现简单片段时已停止

时间:2015-10-16 06:58:36

标签: java android android-fragments

我的主要活动内容是:

公共类MainActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    my_frag myFrag = new my_frag();
    fragmentTransaction.replace(android.R.id.content,myFrag);
    fragmentTransaction.commit();
}

片段类是:

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

主要xml是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

   <fragment
        android:layout_width="2000dp"
        android:layout_height="250dp"
        android:name="com.example.gcoeara.mywork.my_fragment"
        android:id="@+id/fragment"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/fragment_my_frag" />

</RelativeLayout>

命名空间如下:

activity name : MainActivity.java
main xml : activity_main.xml
fragment name : my_frag.java
fragment class : fragment_my_frag.xml

系统:windows 7 框架:android studio

log cat is:

Process: com.example.gcoeara.mywork, PID: 22847
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gcoeara.mywork/com.example.gcoeara.mywork.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
         Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
                at android.app.Activity.setContentView(Activity.java:2144)
                at com.example.gcoeara.mywork.MainActivity.onCreate(MainActivity.java:22)
                at android.app.Activity.performCreate(Activity.java:5933)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
         Caused by: java.lang.IllegalStateException: Fragment com.example.gcoeara.mywork.my_fragment did not create a view.
                at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2145)
                at android.app.Activity.onCreateView(Activity.java:5282)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
                at android.app.Activity.setContentView(Activity.java:2144)
                at com.example.gcoeara.mywork.MainActivity.onCreate(MainActivity.java:22)
                at android.app.Activity.performCreate(Activity.java:5933)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:0)

更改

android:name="com.example.gcoeara.mywork.my_fragment"
你片段中的

android:name="com.example.gcoeara.mywork.my_frag"

希望您将my_frag的包路径视为
com.example.gcoeara.mywork.my_frag。