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 9088003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:49:36+00:00 2026-06-16T21:49:36+00:00

I have a fragment with the following layout: my_fragment.xml <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android

  • 0

I have a fragment with the following layout: my_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/appName"
        android:id="@+id/textView" android:layout_gravity="center"
        android:layout_margin="5dp"/>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:id="@+id/button1"
        android:layout_gravity="center"/>

and corresponding MyFragment.java

package com.example.appdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.top_bar, container, false);

    Button button1 = (Button)view.findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Button Pressed", Toast.LENGTH_SHORT).show();
        }
    });

    return view;
    }
}

and this is hosted inside MainActivity.java

package com.example.appdemo;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

with the following layout activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/fragment"
    android:name="com.example.appdemo.MyFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

Now, my question is why does this only work when I extend FragmentActivity? I’d like to extend Activity. How do I accomplish that?

  • 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-16T21:49:38+00:00Added an answer on June 16, 2026 at 9:49 pm

    Why does this only work inside a host class that extends FragmentActivity?

    Because you are inheriting from android.support.v4.app.Fragment instead of android.app.Fragment.

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

Sidebar

Related Questions

I have the following XML layout: <LinearLayout android:id=@+id/map_locations_list android:layout_width=300dp android:layout_height=fill_parent android:layout_marginLeft=8dp android:layout_marginTop=8dp android:layout_marginBottom=8dp android:background=@color/black_shadow
I have the following application layout: Activity with a LinearLayout hosting a Fragment, which
I have the following code: import android.support.v4.app.FragmentActivity; import android.support.v4.app.Fragment; public void onCreate(Bundle savedInstanceState) {
I have following web-fragment.xml in a jar file, under META-INF/ It seems I cannot
I have tho following code fragment: from xml.etree.ElementTree import fromstring,tostring mathml = fromstring(input) for
I have the following XML fragment: ... <Setting name=HOST1>server1</Setting> <Setting name=HOST2>server2</Setting> <Setting name=HOST3>server4</Setting> <Setting
I have found the following fragment in the Linux kernel (not the corresponding C
I have a fragment shader with the following attributes: varying highp vec2 coordinate; precision
I have to following code fragment, and no matter what I set the font-size
We have the following code fragment: char tab[2][3] = {'1', '2', '\0', '3', '4',

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.