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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:23:39+00:00 2026-06-16T07:23:39+00:00

Code snippets below, public class RelativeActivity extends Activity { // private Button ok_btn =

  • 0

Code snippets below,

public class RelativeActivity extends Activity
{

//  private Button ok_btn = (Button) findViewById(R.id.ok);
//  private EditText edit = (EditText) findViewById(R.id.editText01);
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_relative);

    Button ok_btn = (Button) findViewById(R.id.ok);
    final EditText edit = (EditText)findViewById(R.id.editText01);
    ok_btn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Toast.makeText(getApplicationContext(), edit.getText().toString(), Toast.LENGTH_LONG).show();

        }
    });
}

which is right, however if i set both this instance of Button, ok_btn, and this instance of EditText, edit as private field, as shown in the commented out lines at top of the code, this would result in an error,why?
Thank you 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-16T07:23:40+00:00Added an answer on June 16, 2026 at 7:23 am
    //  private Button ok_btn = (Button) findViewById(R.id.ok);
    //  private EditText edit = (EditText) findViewById(R.id.editText01);
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
      // your code..
    

    this code make an error at run time because you are trying two access Button and EditText before setting an Layout for Current Window (Activity). and in default Activity layout these to Ui elements not exits that’s why you are getting Error.

    solution if you just declare all UI elements before onCreate method of Activity an initialize all UI elements after setting Activity layout in which Ui elements you have added .

    for example in your code :

    public class RelativeActivity extends Activity {
    
    //****************** DECLARE ALL UI ELEMENTS HERE ****************/
    private Button ok_btn;
    private EditText edit;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relative);
    
     //***************** INITIALIZE ALL ELEMENTS HERE AFTER setContentView
    
    
         ok_btn = (Button) findViewById(R.id.ok);
         edit = (EditText)findViewById(R.id.editText01);
    
        ok_btn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getApplicationContext(), 
                 edit.getText().toString(), Toast.LENGTH_LONG).show();
    
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See below code snippets class scopes { static int j=20; Console.WriteLine(j); public static void
See the code snippets below: Code 1 public class A { static int add(int
Consider the following code snippet below. class X { public String toString() { return
What would be PHP code equivalent for snippets below: C# Code : class Composite
I am extending ProgressChangedEventArgs. Below is my code snippet: public class Sample : ProgressChangedEventArgs
Given the below setup and code snippets, what reasons can you come up with
OK, I have a login class like the snippet below public class LoginClass {
In code snippet below, declaring the doThings() method as static would make the class
In my little code snippet below I have a wrapper class for a simple
Edit 2: Below is a code snippet based on DuffyMo's response that illustrates how

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.