Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:05:42+00:00 2026-06-17T22:05:42+00:00

I have been following a tutorial on fragments just to get a bit of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:05:44+00:00Added an answer on June 17, 2026 at 10:05 pm

    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 the Fragment class.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Environment: Eclipse, Maven I have been following the Datanucleus JDO Tutorial to no avail
I have been following this tutorial in order to get username authentication with transport
I'm trying to get emberjs working on rails and have been following this tutorial:
I have been following a tutorial on how to write a basic tile map
I have been following the tutorial on Microsofts website and they use GameTime to
I'm a newcomer to Rails, and have been following the tutorial on the rails
I have been following the affableBean tutorial from the NetBeans site located here .
I have been following a short tutorial to build a tab menu on my
I'm new to NHibernate... I have been following this NHibernate Tutorial from Gabriel Schenker
I am new to Ajax and have been following a tutorial from JQuery.com. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.