Android layout 无法在相对布局中添加片段标记
我想在活动的RelativeLayout中添加一个标记。但我得到渲染错误 有关于如何解决问题的帮助吗 编辑:添加崩溃的堆栈跟踪Android layout 无法在相对布局中添加片段标记,android-layout,android-fragments,Android Layout,Android Fragments,我想在活动的RelativeLayout中添加一个标记。但我得到渲染错误 有关于如何解决问题的帮助吗 编辑:添加崩溃的堆栈跟踪 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chandranichatterjee.mapapp/com.example.chandranichatterjee.myapplicationloc.MapsActivityNew}: android.view
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)}
正如您的错误消息所说:
标记允许布局文件在运行时动态包含不同的布局。在版面编辑时,不知道要使用的特定版面。您可以选择编辑布局时要预览的布局
它不知道片段应该显示什么,但是可以忽略它,因为最终的结果确实知道它。你不能只是有这样一个全面的预览
编辑:
根据膨胀碎片的经验,最常见的错误是:
使用:
public class MainActivity extends Activity {
而不是:
public class MainActivity extends FragmentActivity {
并且在清单文件中的
应用程序
外部具有元数据
标记 如果要使用
标记,我认为您需要在片段中添加android:name=“com.example.myapp.YourFragmentHere”
来定义片段
类。您必须添加android:name=“com.example.YourFragmentHere
在fragment
标记中定义一个fragment类。像这样
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.YourFragmentHere"
/>
根据@Nazariy Moshenskiy的建议,我需要在
标签中添加android:name
属性
这就是我的布局(以防将来有人遇到同样的问题)
发布代码更有用,而不是在代码中使用图片。问题是否仍然存在?代码就在图片中。是的,问题仍然存在是的,但我的意思是,将代码作为文本并标记为代码更有用,因此用户最终可以尝试。当你在图片中给出代码时,很少有人愿意投入精力重新键入图片中的所有代码。由于应用程序在setContentView方法上崩溃,因此不能忽略它。如果你的应用程序在某个时候崩溃,你需要添加错误输出和相应的代码,以便我们可以帮助你编辑问题,添加了StackTraceGod,因为我无法跟踪代码中的错误,我必须自由地解决可能出现的问题,我通过在我的答案中增加一个片段来添加最常见的错误
<?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" />