错误:膨胀类片段

时间:2015-10-15 13:20:33

标签: android android-fragments

我是Android应用程序开发的新手,目前正致力于以编程方式将片段添加到活动中。

我收到此错误:

10-15 18:26:32.165 24020-24020/? E/AndroidRuntime: FATAL EXCEPTION: main
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime: Process: com.ranapps.listing_agent_trail1, PID: 24020
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ranapps.listing_agent_trail1/com.ranapps.listing_agent_trail1.MainActivity}: android.view.InflateException: Binary XML file line #30: Error inflating class fragment
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5336)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:873)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class fragment
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:775)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:512)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:409)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:358)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.Activity.setContentView(Activity.java:1968)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at com.ranapps.listing_agent_trail1.MainActivity.onCreate(MainActivity.java:16)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5302)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
10-15 18:26:32.165 24020-24020/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5336) 

我的MainActivity.java:

package com.ranapps.listing_agent_trail1;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.FragmentActivity;


public class MainActivity extends FragmentActivity {

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

        if(findViewById(R.id.footer) != null){

            if(savedInstanceState != null)
                return;
         }

         Footer bottom1 = new Footer();

         bottom1.setArguments(getIntent().getExtras());

         getSupportFragmentManager().beginTransaction()
                  .add(R.id.footer, bottom1).commit();

    }

}

My Footer.java:

public class Footer extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);

       return inflater.inflate(R.layout.footer_bar, container, false);
   }
}

main_activity.xml

 <fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:id="@+id/footer"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
     />

我正在关注http://developer.android.com/training/basics/fragments/creating.html

上的教程

我搜索了所有类似的问题,但没有一个能解决问题。 谢谢。

1 个答案:

答案 0 :(得分:3)

您尚未在android:name标记的布局中指定属性<fragment。它必须包含片段的完整限定路径。 E.g。

 <fragment android:name="com.example.fragments.Footer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/footer"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
 />