简体   繁体   中英

How do I call a Java methods from one class to another class in Android development?

I have two classes which extend Activity and need to call another class method on main class on android development. I did something like subclass sub = new subclass() . It did not work.

In 1st activity class

package org.me.intent_testing;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.content.Intent;

public class FirstActivity extends Activity {

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

   // Button orderButton = (Button)findViewById(R.id.order);
    Button orderButton = new Button(this);
    orderButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        Intent intent = new Intent(FirstActivity.this, secondActivity.class);
        startActivity(intent);
      }

    });
  }
}

In secondActivity class

package org.me.intent_testing;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.content.Intent;

public class secondActivity extends Activity {

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


   // Button orderButton = (Button) findViewById(R.id.end);
    Button orderButton =  new Button(this);
    orderButton.setOnClickListener(new View.OnClickListener() {

     @Override
      public void onClick(View view) {
        finish();
      }

    });
  }
}

In AndroidManifest.xml

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="org.me.intent_testing">
    <application>
         <activity android:name=".FirstActivity" android:label="FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
         <activity android:name=".secondActivity" />
    </application>

</manifest>

In R.java

package org.me.intent_testing;

public final class R {
    public static final class attr {
    }
    public static final class layout {
        public static final int main=0x7f020000;
    }
    public static final class string {
        public static final int app_name=0x7f030000;
    }
}

I have a problem linking between two classes.

SubClass sub=new SubClass(); is creating an object of class SubClass .

It is not a method calling.

enter code here

If the method is defined in class SubClass then you can call it by using its object and dot operator.

sub.MethodName();

May i know why you would want to do so? If that method is so important to both of the classes, then create it inside a helper class (Which doesn't extend Activity) and create an object of it in both the classes, then with that object access that method.

Try just to label the required method as static. But remember that in that case the called method should not depends on any non-static member variables of methods of the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM