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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:34:25+00:00 2026-06-06T18:34:25+00:00

I have one class called Global and two other activities. In each activity I

  • 0

I have one class called “Global” and two other activities. In each activity I want to create an instance of class Global for reading the first line of a text file called “textfile”. For some reason, it does not work

Here is the code of Global class (in file Global.java):

import android.app.Activity;
public class Global extends Activity {
    public String line;
    public Global() {
        InputStream file = getResources().openRawResource(R.raw.textfile);
        BufferedReader input = new BufferedReader(new InputStreamReader(file));
        try {
            line = input.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} 

Here are codes for a antivity called “HelloWorld” (in file HelloWorld.java) which has an instance of class Global and is to display of first line of “textfile”

public class HelloWorld extends Activity{   
    Global gb;
    TextView myTV;
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.helloworld);
            gb=new Global();
        myTV = (TextView) findViewById(R.id.textView1);
        myTV.setText("First line is: "+gb.line);
    }

}

  • 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-06T18:34:26+00:00Added an answer on June 6, 2026 at 6:34 pm

    There is not need to unnecessarily have Global extending Activity
    Class.

    What you can do is you pass context in global constructor.

    The correct way would be

    HelloWorld.java

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
        public class HelloWorld extends Activity{   
            Global gb;
            TextView myTV;
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.helloworld);
    
                gb=new Global(this);
    
                myTV = (TextView) findViewById(R.id.textView1);
                myTV.setText("First line is: "+gb.getLine());
            }
        }
    

    Global.java

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    
    public class Global {
        private AbcActivity abcActivity;
        public String line;
    
        public Global(AbcActivity abcActivity) {
            this.abcActivity = abcActivity;
        }
    
        public String getLine() {
            InputStream file = abcActivity.getResources().openRawResource(
                    R.raw.textfile);
            BufferedReader input = new BufferedReader(new InputStreamReader(file));
            try {
                line = input.readLine();
                return line;
            } catch (IOException e) {
                e.printStackTrace();
                return "Error reading File!";
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three files: one called sql.php witch has a class db that I
I have a class User with one field called birthDate which is a java.sql.Date
I have one class. Class First { private Second second; public First(int num, String
I have one abstract class -let's say myBase. And I want all the classes
I have an action in which I want to use one global kind of
I've got a bunch of strongly typed DataSet that each have two tables. One
I have a model called Contact which has_one guest like so. class Contact <
I'm making a program on the HUFFMAN ALGORITHM where I have one class of
I can't figure out why I'm getting this error. I only have one class
I have one main class with several inherited members who all overload the same

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.