I have been following a tutorial on fragments just to get a bit of exposure to them, I’ve followed the tutorial to the end and Eclipse is throwing the error "The method Fragment1() is undefined for the type MainActivity" now I’m not sure if is to do with the Import.R.Android.* problem I was having earlier and its now not referring to Fragment1.class? Or has something been missed on the tutorial initializing the Fragment?
As I understand from
public 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();
}
}
It replaces the placeholder Fragment(newFragment) with a fragment based off which button was pressed? Or am I missing something that should obvious? Thank you for any help.
<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: I know its because the fragment is not defined but since I was following a tutorial I don’t know yet how to define one.
The Android developer guides do not help with this particular case as they seem to be implementing them in a different way to this tutorial.
I would suggest finding a nice book or website on how to code in Java before attempting to learn the Android framework, as it seems you have some problems with Java classes and some basic Java syntax.
To Java,
newFragment = Fragment1();is a method call. It has a method name (Fragment), and and is passing in no arguments.To instantiate an object (such as a Fragment), you need to use the new keyword. For example,
Fragment myFragment = new Fragment1();.Furthermore, it isn’t clear from your code snippets, but you must have classes defined of types
Fragment1,Fragment2, etc. These should extend theFragmentclass.I highly recommend following the Android developers guide, as the guide you are following doesn’t seem to be helping you much, and the official guide provides very nice examples of how Google says you should organize and code your application.