无法在相对布局中添加片段标签

3
我想在Activity的RelativeLayout内添加一个标签。但是我遇到了渲染错误。

fragment tag

任何有关解决问题的帮助吗?
编辑:添加崩溃的堆栈跟踪
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)}

更有帮助的做法是发布代码,而不是使用代码图片。问题仍然存在吗? - jle
代码就在图片里。是的,问题仍然存在。 - Molly
1
是的,但我的意思是将代码作为文本并标记为代码会更有帮助,这样用户最终可以尝试它。当您在图片中提供代码时,很少有人愿意花费精力从图像中重新输入所有代码。 - jle
4个回答

3

正如@Nazariy Moshenskiy所建议的那样,我需要在<fragment>标记中添加android:name属性。

以下是我的布局外观(以防未来有人遇到相同的问题)。

<?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" />

2

如果你要使用<fragment>标签,需要在你的片段中添加android:name="com.example.myapp.YourFragmentHere"来定义一个Fragment类。

请注意保留HTML标签。

1

正如您的错误消息所示:

<fragment> 标签允许布局文件在运行时动态包含不同的布局。在编辑布局时,尚未确定要使用的特定布局。您可以在编辑布局时选择要预览的布局。

它不知道 Fragment 应该显示什么,但可以忽略它,因为最终结果确实知道它。您不能只有这样一个普遍的预览。

编辑:

从经验中得出的填充片段的最常见错误是:

使用:

public class MainActivity extends Activity {

改为:

public class MainActivity extends FragmentActivity {

并且在清单文件中,meta-data标签位于application之外。


无法忽略,因为在setContentView方法上应用程序会崩溃。 - Molly
如果您的应用程序在某个时刻崩溃,您需要添加错误输出和相应的代码,以便我们可以帮助您。 - jle
编辑了问题,添加了堆栈跟踪。 - Molly
好的,既然我无法追踪你代码中的错误,我不得不自由地射击可能存在的问题,我在我的答案中添加了充气片段的最常见错误。 - jle

1

您需要在fragment标签内添加android:name="com.example.YourFragmentHere"来定义一个片段类,就像这样:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接