cmac
cmac

Reputation: 916

why is the button instantiated in the second instance but not the first?

I have to ask this because I'm really struggling with OOP, and these are OOP concepts that I'm not understanding. Please help me. The code I've pasted below comes from the onCreate method for an Android Activity, but you may be able to answer even without knowing Android. Why isn't a button object instantiated for this button:

buttonSaveMem2 = (Button)findViewById(R.id.save_mem2);
buttonSaveMem2.setOnClickListener(buttonSaveMem2OnClickListener);

while an object is created for this one?

Button buttonStartAnother = (Button)findViewByI(R.id.startanother);        
buttonStartAnother.setOnClickListener(new Button.OnClickListener()
{

Here is the full code for the method:

   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
   textSavedMem2 = (TextView)findViewById(R.id.savedmem2);
   editText1 = (EditText)findViewById(R.id.edittext1);
   editText2 = (EditText)findViewById(R.id.edittext2);
   buttonSaveMem1 = (Button)findViewById(R.id.save_mem1);
   buttonSaveMem2 = (Button)findViewById(R.id.save_mem2);

   buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);
   buttonSaveMem2.setOnClickListener(buttonSaveMem2OnClickListener);

   Button buttonStartAnother = (Button)findViewById(R.id.startanother);
   buttonStartAnother.setOnClickListener(new Button.OnClickListener(){

Thank you.

Upvotes: 0

Views: 97

Answers (3)

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

What I understood from your question and code posted here is you are just asking the difference between below two statements? and also why the statements were written like that?

buttonSaveMem2 = (Button)findViewById(R.id.save_mem2);

and

Button buttonStartAnother = (Button)findViewByI(R.id.startanother);

Am I correct ? If yes, then read below answer otherwise forget it.

You are thinking the button buttonStartAnother instantiated as in second statement and button buttonSaveMem2 not instantiated as in first statement.

Actually the button buttonSaveMem2 declared before onCreate(). Why he did like this because, he has to use the button buttonSaveMem2 in other methods.

But button buttonStartAnother is not required to use in any other methods, so he declared inside onCreate().

See below template for your code you will understand this.

public class Demo extends Activity {
    TextView textSavedMem1, textSavedMem2;
    Button buttonSaveMem1, buttonSaveMem2;
    EditText editText1, editText2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
        textSavedMem2 = (TextView)findViewById(R.id.savedmem2);
        editText1 = (EditText)findViewById(R.id.edittext1);
        editText2 = (EditText)findViewById(R.id.edittext2);
        buttonSaveMem1 = (Button)findViewById(R.id.save_mem1);
        buttonSaveMem2 = (Button)findViewById(R.id.save_mem2);

        buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);
        buttonSaveMem2.setOnClickListener(buttonSaveMem2OnClickListener);

        Button buttonStartAnother = (Button)findViewById(R.id.startanother);
        buttonStartAnother.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub              
            }   
        });
    }       
    public void myMethod() {
        buttonSaveMem2.setText("myMethod called");
            // buttonSaveMem2 is accessible here because it is declared globally
        // buttonStartAnother.setText("hello");
            // If you remove comment on above line you will get error
            // because buttonStartAnother is invisible to this method
    }
}

I hope you will understand why the buttons are instantiated like that, if you didn't check myMethod in above code check that.

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234807

The problem is almost surely that your layout is missing a view with id save_mem2. The reason the second code block works is that there is a view with id startanother.

Upvotes: 0

jtt
jtt

Reputation: 13541

This is not an OOP problem, its in fact a coding problem with your implementation.

Read the basics of android... A widget is a view that is used by android to take input from the user, this can be a click or whatever. Specifically due to the way android is designed, all widgets(views) when being used must have some way to reference them... enter the method fineViewById(xx) this is used to find an widget/view with this particular id, often times coming from the R.java file.

Learn the basics of the language before you start coding this will save you a lot of time..

You can start here -> http://developer.android.com/resources/browser.html?tag=tutorial

Upvotes: 0

Related Questions