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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:21:30+00:00 2026-06-14T09:21:30+00:00

Hi Im trying to go from an activity to a fragment. Here’s the code

  • 0

Hi Im trying to go from an activity to a fragment.

Here’s the code that I have in my Activity

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TableLayout;
import android.widget.TextView;


public class SummaryActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SimpleBookManager testBook = new SimpleBookManager();
        setContentView(R.layout.summary);


        TableLayout tableSummary = (TableLayout) this.findViewById(R.id.tableSummary);
        tableSummary.setBackgroundResource(R.drawable.book2);

        TextView nrOfBooksfield = (TextView) this.findViewById(R.id.nrOfBooksInCollection);   
        String text = Integer.toString(testBook.count());
        nrOfBooksfield.setText(text);


    }
}

And hers my fragment

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class BFragmentTab extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.summary, container, false);

    }

SimpleBookManager testBook = new SimpleBookManager();
TableLayout tableSummary = (TableLayout) getView().findViewById(R.id.tableSummary);
tableSummary.setBackgroundResource(R.drawable.book2);
TextView nrOfBooksfield = (TextView) getView().findViewById(R.id.nrOfBooksInCollection);
String text = Integer.toString(testBook.count());
nrOfBooksfield.setText(text);
}

Now it complains on tableSummary.setBackgroundResource(R.drawable.book2); saying that it is syntax error on “setBackgroundResource” and the “.” Identifier expected.

And there is another error on nrOfBooksfield.setText(text); “misplaced construct on “.” and syntaxerror on token “text” VariableDeclarationId expected after this token.

Whats the error, it worked fine in the Activity and now it complains in the fragment (and yes Im a newbie on Android).

Thanks in advance.

  • 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-14T09:21:31+00:00Added an answer on June 14, 2026 at 9:21 am

    Inflate a View and place your code inside onCreateView()

       public class BFragmentTab extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.summary, 
        SimpleBookManager testBook = new SimpleBookManager();
        TableLayout tableSummary = (TableLayout) view.findViewById(R.id.tableSummary);
        tableSummary.setBackgroundResource(R.drawable.book2);
        TextView nrOfBooksfield = (TextView) view.findViewById(R.id.nrOfBooksInCollection);
        String text = Integer.toString(testBook.count());
        nrOfBooksfield.setText(text);
    
        return inflater.inflate(R.layout.summary, container, false);
                    }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a Spinner View from an added Fragment to my activity
I'm trying to launch an activity from a custom lisview. I have hoded the
I am trying to write a simple activity that received from the user its
I'm implementing some code from an example (http://arvid-g.de/12/android-4-actionbar-with-tabs-example) and trying to convert it to
I'm trying to have an Activity that shows just a few (or one) preference
I'm trying to stand up a simple Android app that uses a camera via
Trying to call a method in my activity from a fragment. I want the
I'm new on Android, and I'm trying to start an Activity from a MenuItem
I am trying to have a tabhost that it contained in a fragment, where
I am trying to start the main activity from inside a BroadcastReceiver. I dont

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.