Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old November 30th, 2010, 11:37 PM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Nov 2010
Posts: 5
 
Device(s): HTC Hero
Carrier: Not Provided

Thanks: 1
Thanked 1 Time in 1 Post
Confused Global declarations

Hi all

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 Code:

   
public void printf(String arg,TextView to){
        
TextView console = (TextViewfindViewById(R.id.TextView02);
        
to.setText(getConsole()+"\n# "+arg);
        
scroll();    
        }

    public 
String getConsole(){
        
TextView console = (TextViewfindViewById(R.id.TextView02);
        return(
console.getText().toString());
        } 
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!

loxaXcracker is offline  
Reply With Quote
sponsored links
Old December 1st, 2010, 01:57 AM   #2 (permalink)
New Member
 
Join Date: Aug 2010
Posts: 12
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
galth is offline  
Reply With Quote
Old December 1st, 2010, 02:06 AM   #3 (permalink)
New Member
Thread Author (OP)
 
Join Date: Nov 2010
Posts: 5
 
Device(s): HTC Hero
Carrier: Not Provided

Thanks: 1
Thanked 1 Time in 1 Post
Default

Thanks for the reply.

I would also love to know that.

A simple:

PHP Code:
package test.tests;

public class 
tests extends Activity{
@
Override

    Button Btest 
= (Button)this.findViewById(R.id.Button01);

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

causes a force close and doesn't give any syntax error.
loxaXcracker is offline  
Reply With Quote
Old December 1st, 2010, 02:36 AM   #4 (permalink)
Member
 
Join Date: Apr 2010
Location: Netherlands
Posts: 142
 
Device(s): HTC Desire
Carrier: Not Provided

Thanks: 1
Thanked 23 Times in 23 Posts
Default

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

try this:
PHP Code:
package test.tests;

public class 
tests extends Activity{
@
Override

    Button Btest 
null;

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

        
this.Btest = (Button)this.findViewById(R.id.Button01);

now you execute the 'findViewById()' after you have set the layout with 'setContentView()'
id0001 is offline  
Reply With Quote
The Following User Says Thank You to id0001 For This Useful Post:
loxaXcracker (December 1st, 2010)
Old December 1st, 2010, 02:44 AM   #5 (permalink)
New Member
Thread Author (OP)
 
Join Date: Nov 2010
Posts: 5
 
Device(s): HTC Hero
Carrier: Not Provided

Thanks: 1
Thanked 1 Time in 1 Post
Default

Ahh thanks a bunch, I think that's exactly what I needed
loxaXcracker is offline  
Reply With Quote
Old December 1st, 2010, 03:11 AM   #6 (permalink)
New Member
Thread Author (OP)
 
Join Date: Nov 2010
Posts: 5
 
Device(s): HTC Hero
Carrier: Not Provided

Thanks: 1
Thanked 1 Time in 1 Post
Default

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 Code:
public class tools extends Utilities{
//Different functions here
public void foo(){}

tools.this.foo(); gives this error however:

Quote:
No enclosing instance of the type tools.foo is accessible in scope
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!
loxaXcracker is offline  
Reply With Quote
Old December 1st, 2010, 03:35 AM   #7 (permalink)
Member
 
Join Date: Apr 2010
Location: Netherlands
Posts: 142
 
Device(s): HTC Desire
Carrier: Not Provided

Thanks: 1
Thanked 23 Times in 23 Posts
Default

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
id0001 is offline  
Reply With Quote
Reply
Tags
functions, global, java, variables


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 02:35 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.