Global declarations


Last Updated: 2010-12-01 09:35:14
  1. loxaXcracker

    loxaXcracker Member

    Hi all :rolleyes:

    I'm a rather experienced programmer, but this is my first contact with Java.
    I've been trying to figure out a way to not have to declare my Variables, Methods etc in every function (or whatever they're called in Java).

    Here's an example:

    PHP:
    1.  
    2.  
    3.    public void printf(String arg,TextView to){
    4.         TextView console = (TextView) findViewById(R.id.TextView02);
    5.         to.setText(getConsole()+"\n# "+arg);
    6.         scroll();    
    7.         }
    8.  
    9.     public String getConsole(){
    10.         TextView console = (TextView) findViewById(R.id.TextView02);
    11.         return(console.getText().toString());
    12.         }
    13.  
    14.  
    This is just a silly example to show you that I had two define "console" twice. If I attempt to define it outside of a function (e.g. right inside of the class), the application will Force Close before it's even started.

    Please help.

    Thanks in advance!

    Advertisement
  2. galth

    galth Member

    The specific example you gave would normally be declared in the class of some type of view or activity and not in each individual method. Then the assignment of the console variable would normally occur in the onFinishInflate() method in a view class or after you've inflated the view in an activity class, usually in the onCreate method. I'm not sure why the app would force close in any case.
  3. loxaXcracker

    loxaXcracker Member

    Thanks for the reply.

    I would also love to know that.

    A simple:

    PHP:
    1.  
    2. package test.tests;
    3.  
    4. public class tests extends Activity{
    5. @Override
    6.  
    7.     Button Btest = (Button)this.findViewById(R.id.Button01);
    8.  
    9.     public void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.         setContentView(R.layout.main);
    12. }
    13.  
    14.  
    causes a force close and doesn't give any syntax error.
  4. id0001

    id0001 Well-Known Member

    that is because when you execute the findViewById() method, your activity doesn't have a layout yet.

    try this:
    PHP:
    1.  
    2. package test.tests;
    3.  
    4. public class tests extends Activity{
    5. @Override
    6.  
    7.     Button Btest = null;
    8.  
    9.     public void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.         setContentView(R.layout.main);
    12.  
    13.         this.Btest = (Button)this.findViewById(R.id.Button01);
    14. }  
    15.  
    now you execute the 'findViewById()' after you have set the layout with 'setContentView()'
    loxaXcracker likes this.
  5. loxaXcracker

    loxaXcracker Member

    Ahh thanks a bunch, I think that's exactly what I needed :D
  6. loxaXcracker

    loxaXcracker Member

    I hope you don't mind explaining another thing:

    If I create another class and use "extends", I can inherit everything from the parent class.

    For example I'd like to create a secondary class "Tools" where I'd have mini utilities that I'd like to invoke through the parent.

    PHP:
    1.  
    2. public class tools extends Utilities{
    3. //Different functions here
    4. public void foo(){}
    5. }
    6.  
    tools.this.foo(); gives this error however:

    I tried lots of things such as public, private, abstract, final, static etc but I still can't find it. I guess I'd have to somehow make the parent also inherit from the child?

    Thanks in advance again!
  7. id0001

    id0001 Well-Known Member

    well there is a way: Calling Subclass Methods
    But you shouldn't need to call methods in a subclass from your parent class. That's bad OO

Share This Page