Reputation: 73
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.
Upvotes: 1
Views: 1442
Reputation: 2129
SubClass sub=new SubClass();
is creating an object of class SubClass
.
It is not a method calling.
If the method is defined in class SubClass then you can call it by using its object and dot operator.
sub.MethodName();
Upvotes: 2
Reputation: 11251
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.
Upvotes: 2
Reputation: 43108
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.
Upvotes: 0