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

  • Home
  • SEARCH
  • 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 6221441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:08:04+00:00 2026-05-24T08:08:04+00:00

Hi im new to android and I have a program that has a global

  • 0

Hi im new to android and I have a program that has a global variable define and it works, so I can set it and get it in every activity, BUT it dosnt like to be changed in an on click listener. I made it so on the screen there is an edittext and when someone presses a button I want the edittext text to be put into the global variable. here is my code:

Button SiteButton = (Button) findViewById(R.id.SiteButton);
    SiteButton.setOnClickListener(new View.OnClickListener() {
        TextView textviewS = (TextView) findViewById(R.id.SiteIdT);
        EditText edittextS = (EditText) findViewById(R.id.SiteIdE);
        TextView textviewB = (TextView) findViewById(R.id.BusIdT);
        EditText edittextB = (EditText) findViewById(R.id.BusIdE);
        public void onClick(View v) {
            textviewS.setText(edittextS.getText());
            ((Global) this.getApplication()).setgSiteId(textviewS.getText().toString());
            textviewB.setText(edittextB.getText()); 
            ((Global) this.getApplication()).setgVehicleId(textviewB.getText().toString());
        }
    });

but the getApplication() part is showing an error. can anyone help?

  • 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-05-24T08:08:05+00:00Added an answer on May 24, 2026 at 8:08 am

    You should refer to your activity this, since View.OnClickListener doesn’t have such a method:

    // Bad code! read below
    ((Global) MyActivityClassName.this.getApplication()).setgSiteId(textviewS.getText().toString());
    textviewB.setText(edittextB.getText()); 
    ((Global) MyActivityClassName.this.getApplication()).setgVehicleId(textviewB.getText().toString());
    

    By the way, how do you cast the return from getApplication() to Global? You will get a class cast exception there.

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

Sidebar

Related Questions

In my Android program, I have some code that downloads a file. This works
I have an android activity and a service implemented using aidl. Works like a
R class on android has it's limitations. You can't use the resources dynamically for
I have an Android application (java) that was working fine when compiled with the
I'm new to Android, I've followed the hello world tutorial through and have a
I'm new to Java programming, I am programming Java 1.6 with Android. I have
New to WCF, but familiar with COM+ - can I wrap a WCF service
Being new to test based development, this question has been bugging me. How much
We are creating an app for a client that has hundreds of megabytes of
My program does some network activity in a background thread. Before starting, it pops

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.