![](/img/trans.png)
[英]"the method getSupportFragmentManager() is undefined for the type MainActivity"
[英]Fragments, The method Fragment1() is undefined for the type MainActivity I would like explaining please
我一直在关注有关片段的教程,只是为了稍微了解一下它们,所以我一直遵循该教程的结尾,Eclipse抛出了错误"The method Fragment1() is undefined for the type MainActivity"
现在不知道是否与我之前遇到的Import.R.Android.*
问题有关,现在不参考Fragment1.class?
还是在初始化Fragment的教程中遗漏了某些内容?
据我从公开void Onclick(View v){
Fragment newFragment;
if (v == button1) {
newFragment = Fragment1();
}else if (v == button2) {
newFragment = Fragment2();
}else if (v == button3) {
newFragment = Fragment3();
}else {
newFragment = StartFragment();
}
}
它将基于按下哪个按钮的片段替换占位符Fragment(newFragment)
。 还是我缺少一些显而易见的东西? 感谢您的任何帮助。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Onclick"
android:text="Frag1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Onclick"
android:text="Frag2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="OnClick"
android:text="Frag3" />
</LinearLayout>
<LinearLayout
android:id="@+id/myFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
TLDR:我知道它是因为片段没有定义,但是由于我正在学习一个教程,所以我还不知道如何定义它。
Android开发人员指南对这种特殊情况没有帮助,因为它们似乎以与本教程不同的方式来实现它们。
在尝试学习Android框架之前,我建议找一本不错的书或网站,介绍如何用Java编写代码,因为您似乎在Java类和一些基本Java语法方面存在问题。
对于Java, newFragment = Fragment1();
是一个方法调用。 它具有方法名称( Fragment
),并且没有传入任何参数。
要实例化一个对象(例如Fragment),您需要使用new关键字。 例如, Fragment myFragment = new Fragment1();
。
此外,从代码片段中还不清楚,但是您必须定义类型为Fragment1
, Fragment2
等的类。这些类应该扩展Fragment
类。
我强烈建议您遵循Android开发人员指南 ,因为您所遵循的指南似乎并没有太大帮助,并且官方指南提供了非常好的示例,说明了Google如何组织和编写应用程序代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.