unable to add a fragment tag inside a relative layout

Viewed 685

I want to add a tag inside a RelativeLayout of an Activity. But i am getting rendering errors.

fragment tag

Any help on how to solve?

Edit: Adding the stacktrace of the crash

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chandranichatterjee.mapapp/com.example.chandranichatterjee.myapplicationloc.MapsActivityNew}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2583)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5767)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
     Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class fragment
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
        at android.app.Activity.setContentView(Activity.java:2204)
        at com.example.chandranichatterjee.myapplicationloc.MapsActivityNew.onCreate(MapsActivityNew.java:24)
        at android.app.Activity.performCreate(Activity.java:6322)}
4 Answers

As suggested by @Nazariy Moshenskiy , I needed to add android:name attribute to the <fragment> tag.

This is how my layout looked like(in case someone encounters the same problem in future).

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraZoom="2"

    tools:context=".MapsActivityNew" />

I think you need to add android:name="com.example.myapp.YourFragmentHere" inside your fragment to define a Fragment class if you are going to use <fragment> tag.

As your error message says:

A <fragment> tag allows a layout file to dynamically include different layouts at runtime. At layout editing time the specific layout to be used is not known. You can choose which layout you would like previewed while editing the layout.

It doesn't know what the Fragment should show, but it can be ignored, since the result in the end does know it. You can't just have such a general preview.

Edit:

Most common mistakes from experience with inflating a fragment are:

using:

public class MainActivity extends Activity {

instead of:

public class MainActivity extends FragmentActivity {

and having meta-data tag outside the application in the manifest file.

You have to add android:name="com.example.YourFragmentHere inside your fragment tag to define a Fragment class. like this

<fragment  
android:id="@+id/fragment"
android:layout_width="match_parent"  
android:layout_height="match_parent"  
android:name="com.example.YourFragmentHere"  
/> 
Related