开发者

Android: OOP | Using functions in other files

I'm pretty new to Android but I have some experience (and a bit rusty with) Java and OOP.

Basically what my app does is when you press a button, led's or "images" will flash.

I'm trying to divide up my project into multiple files where I can just import a java file to use the class's functions but I'm unsure on how to do that...

I have two files. 1) HelloFormStuff.java (this is like the main one), and 2) led_functions.java

In HelloFormStuff I put:

import com.example.helloformstuff.led_functions
// com.example.helloformstuff is the package

Example of code.. HelloFormStuff.java

    public class HelloFormStuff extends Activity {

/* Called when the activity is first created. */
@Override
    public void onCr开发者_如何学运维eate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button btn1 = (Button) findViewById(R.id.btn1);
    btn1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            currentMode.setText("Button 1 Pressed");
            led_circleBusy();
           }
    });
}

}

And in led_functions.java...

public class led_functions extends Activity{

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

    final ImageView LED_1;
    LED_1 = (ImageView)findViewById(R.id.LED_1);

   @Override 
    public void led_circleBusy()
    {
        LED_1.setVisibility(View.INVISIBLE);

    }

}

An error I get is: The method led_circleBusy() is undefined for the type new View.OnClickListener(){}

So I'm asking is how to implement my other functions from other files.

Thanks.

-Paul

PS: I've been looking at sample code and if you're wondering why I put something like an "@Override" where it's not needed, just ignore it.. I've been trying random things :l


Well, it looks like you'll have to go back studying:

  1. Java/OOP
  2. Android applications lifecycles

    1. Java/OOP

In Java, you can't define a method inside another method the way you did with led_circleBusy() inside onCreate(). Your led_functions class can't compile as is.

Then, you would have to either declare your led_circleBusy() in order to call it from anywhere using led_functions.led_circleBusy() or create a new instance of led_functions (new led_functions()) before invoking led_circleBusy() on it.

  1. Android application lifecycles

Activities in android applications can be considered as "screens". 2 Activities can only communicates via Intents. An action on a View in one Activity can modify a View on a second Activity only by passing an intent from Activity A to Activity B.

If your components are on the same screen, then you should not have 2 subclasses of Activity but only one and a utility class to which you pass parameters from your Activity.

You should read really carefully http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle

And then, read it again.

The second thing you should read carefully in android dev guide is about Intents: http://developer.android.com/guide/topics/intents/intents-filters.html

These concepts are new to any developer, experienced in Java or not. So you have to spend the time necessary for you to understand them in details to be able to create the most basic android app.


You can do the following from your HelloFormStuff class.

led_functions led = new led_functions();
led.led_circleBusy();

Then get rid of the @Override from above the led_circleBusy() method. You shouldn't really be calling other Activity's methods like that though. Is led_functions really an Activity/UI class? If not, remove the extends Activity, add LED_1 to HelloFormStuff and then pass it as a parameter to led.led_circleBusy(ImageView led1).


As long as all classes (Android specific or otherwise) occupy the same package / namespace, e.g., com.mycompany.mypackage then no imports are necessary...

For the helper...

package com.mycompany.mypackage

public class MyHelper {

    public void doSomething() {
        ...
    }

}

For the Activity...

package com.mycompany.mypackage

public class MyActivity extends Activity {

    private MyHelper helper = new MyHelper();

    @Override
    protected void onCreate(...) {
        ...

        helper.doSomething();

}


HelloFormStuff class has no led_circleBusy() implementation, so this method couldn't be called


You have two ways.

  1. Declare the led_circleBusy() as static, then you can call it as in led_functions.led_circleBusy()

  2. Keep it non-static and call it on an instance of led_functions as in

    led_functions lf = new led_functions(); lf.led_circleBusy();

This is strictly java-speaking. If they are actually two different Activitys (different 'windows') that share nothing, but one calls another, you should take a look at Intents.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜