eclipse Android ADT:没有fragment_main.xml,只有activity_main.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22586270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ADT: No fragment_main.xml, only activity_main.xml
提问by john.zli
I'm trying to learn how to program android applications, so I downloaded the ADT bundle that google supplied, and I tried following the tutorial that allowed me to create a simple application. However, during the procedures, there are several instructions telling me to open up the fragment_main.xml file, but my layout/res/ directory did not have this file, only the activity_main.xml file. Furthermore, when creating new Android activities, there was never an option to name my fragment layout, indicating that eclipse just doesn't create it for some reason. I didn't think this would be an issue at first (I just edited activity_main instead), until I realized that the tutorial wanted us to use the some information from the fragment class or xml file.
我正在尝试学习如何编写 android 应用程序,所以我下载了谷歌提供的 ADT 包,并尝试按照允许我创建一个简单应用程序的教程进行操作。但是,在过程中,有几条指令告诉我打开fragment_main.xml文件,但是我的layout/res/目录没有这个文件,只有activity_main.xml文件。此外,在创建新的 Android 活动时,从来没有一个选项来命名我的片段布局,这表明 eclipse 只是出于某种原因没有创建它。一开始我并不认为这会是一个问题(我只是编辑了 activity_main),直到我意识到教程希望我们使用片段类或 xml 文件中的一些信息。
Does anyone know why my Eclipse IDE is not creating a fragment_main.xml? I will try to supply more details if necessary.
有谁知道为什么我的 Eclipse IDE 没有创建 fragment_main.xml?如有必要,我将尝试提供更多详细信息。
采纳答案by Prabindh
Based on the versions you indicated in the comment response, I think updating to the later versions (22.6+) would help, as discussed in https://code.google.com/p/android/issues/detail?id=67421
根据您在评论回复中指出的版本,我认为更新到更高版本(22.6+)会有所帮助,如https://code.google.com/p/android/issues/detail?id=67421 中所述
回答by goyalshub1509
While creating the new Android Project in one of the panels select "Blank activity with Fragment" instead of default selection "Blank Activity". The Android Developer tutorial does not say anything about it. Doing so will create the file fragment_main.xml in res/layout/ which is needed to continue subsequent steps.
在其中一个面板中创建新的 Android 项目时,选择“带片段的空白活动”而不是默认选择“空白活动”。Android 开发者教程对此没有任何说明。这样做将在 res/layout/ 中创建文件 fragment_main.xml,这是继续后续步骤所需的。
回答by intellectual_stretch
Try creating a new project with "Blank Activity with Fragment". I hope this is helpful.
尝试使用“带片段的空白活动”创建一个新项目。我希望这是有帮助的。
回答by EngineerLuna
Open MainActivity.java from the src/(package name file)/ directory. Then inside the java file there is a method called OnCreate() that has setContentView(R.layout.activity_main) as default.
从 src/(package name file)/ 目录打开 MainActivity.java。然后在 java 文件中有一个名为 OnCreate() 的方法,它默认设置了 setContentView(R.layout.activity_main)。
Change that to R.layout.activity_main to R.layout.fragment_main so instead of having setContentView(R.layout.activity_main) you'll have setContentView(R.layout.fragment.main)
将其更改为 R.layout.activity_main 到 R.layout.fragment_main,因此您将拥有 setContentView(R.layout.fragment.main) 而不是 setContentView(R.layout.activity_main)
Once that's done change the name of the activity_main.xml file under the /res/layout/ directory to fragment_main.xml
完成后,将 /res/layout/ 目录下的 activity_main.xml 文件的名称更改为 fragment_main.xml
Thanks for that Onik!
谢谢你的奥尼克!