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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:18:58+00:00 2026-06-01T00:18:58+00:00

I am pretty new to android app development and java and I encountered the

  • 0

I am pretty new to android app development and java and I encountered the following error when trying to call a method from another class:

Cannot make a static reference to the non-static method showToast(String) from the type LoginActivity

The reason I want to call this in the other class and not instantiate it in the class itself is because of adaptability and upgradeability issues. This seems a fine concept by me or is it something that is way overrated?

The relevant code is:

CreateAccountActivity

public class NewAccountActivity extends Activity{

private Button mCreateAccountButton;

 @Override
 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.newaccount);

    mCreateAccountButton = (Button) findViewById(R.id.createaccount_button);

    registerButtonListeners();

    }

    private void registerButtonListeners() {
        mCreateAccountButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginActivity.showToast(getString(R.string.createaccount_message));
            }
        });
    }   
}

LoginActivity

public void showToast(String toastString) {
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

    ImageView image = (ImageView) layout.findViewById(R.id.toastImage);
    image.setImageResource(R.drawable.android);

    TextView text = (TextView) layout.findViewById(R.id.toastText);
    text.setText(toastString);

    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show(); 
}

Creating a new LoginActivity object and then calling showToast on it makes the error messages go away but gives me a NullPointerException instead.

  • 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-01T00:18:59+00:00Added an answer on June 1, 2026 at 12:18 am

    if you want to call showToast in the static way you have to make the method showToast static.

    public static void showToast(...)
    

    Edit: where do you get the NullPointerException?

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

Sidebar

Related Questions

So I'm pretty new to android development and have been trying to piece together
I'm pretty new to Android app development, and I've been playing around with swipe
I am completely new to android, and pretty much a Java newb. I have
I'm pretty new to android development, so I hope my question is easy, but
I'm new in android development and I am trying out the WebView example in
I'm pretty new to Android development, and I have a quick question about a
I am still pretty new to Android development, and I have not been able
I'm pretty new to eclipse and Android and I'm trying to add some files
I'm trying to start a new activity from a non-activity class. From the main
I'm pretty new to Android development and would appreciate some help. All I want

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.