How do I call the method startChronometer in another class when the method is declared inside main activity?
当在主活动中声明方法时,如何调用另一个类中的方法startChronometer ?
Inside MainActivity:
在MainActivity:
public void startChronometer(){
mChronometer.start();
showElapsedTime();
}
Inside another class, I tried to do this:
在另一个课堂上,我试着这样做:
MainActivity mActivity;
mActivity.startChronometer();
But an error occurred which says
但是发生了一个错误
java.lang.NullPointerException.
java.lang.NullPointerException。
May I know what more do I need to add to the code?
我可以知道我还需要添加什么到代码中吗?
10 个解决方案
#1
13
You can easily call a method from any Fragment inside your Activity by just doing a cast like this:
您可以通过以下方式从活动中的任何片段调用方法:
((MainActivity)getActivity()).startChronometer();
Hope this helps!
希望这可以帮助!
#2
8
Initialize it first
首先初始化
MainActivity mActivity= new MainActivity();
Then you can continue
然后你可以继续
mActivity.startChronometer();
#3
8
I would suggest, one should not make object of an Activity type class.
我的建议是,不应该创建活动类型类的对象。
MainActivity mActivity = new MainActivity(); // BIG NO TO THIS.
All Activities in Android must go through the Activity lifecycle so that they have a valid context attached to them.
Android中的所有活动都必须经过活动生命周期,以便它们有一个有效的上下文。
By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.
通过将活动视为一个普通的Java类,您将得到一个空上下文。由于活动中的大多数方法在其上下文中被调用,您将得到一个空指针异常,这就是为什么您的应用程序会崩溃。
Instead, move all such methods which need to be called from other classes into a Utility class which accepts a valid context in its constructor, and then use that context in the methods to do the work.
相反,将需要从其他类调用的所有此类方法移动到实用程序类中,该实用程序类在其构造函数中接受有效上下文,然后在方法中使用该上下文来完成工作。
#4
4
MainActivity.java
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static MainActivity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instance = this;
}
public static MainActivity getInstance() {
return instance;
}
public void myMethod() {
// do something...
}
)
AnotherClass.java
AnotherClass.java
public Class AnotherClass() {
// call this method
MainActivity.getInstance().myMethod();
}
#5
3
But an error occurred which says java.lang.NullPointerException.
但是出现了一个错误,它说java.lang.NullPointerException。
Thats because, you never initialized your MainActivity. you should initialize your object before you call its methods.
这是因为,您从未初始化主活动。在调用它的方法之前,应该初始化对象。
MainActivity mActivity = new MainActivity();//make sure that you pass the appropriate arguments if you have an args constructor
mActivity.startChronometer();
#6
2
Use this code in sub Fragment of MainActivity
to call the method on it.
在MainActivity的子片段中使用此代码调用该方法。
((MainActivity) getActivity()).startChronometer();
#7
0
You have to pass instance of MainActivity into another class, then you can call everything public (in MainActivity) from everywhere.
您必须将MainActivity实例传递到另一个类中,然后您可以从任何地方调用everything public(在MainActivity中)。
MainActivity.java
MainActivity.java
public class MainActivity extends AppCompatActivity {
// Instance of AnotherClass for future use
private AnotherClass anotherClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Create new instance of AnotherClass and
// pass instance of MainActivity by "this"
anotherClass = new AnotherClass(this);
}
// Method you want to call from another class
public void startChronometer(){
...
}
}
AnotherClass.java
AnotherClass.java
public class AnotherClass {
// Main class instance
private MainActivity mainActivity;
// Constructor
public AnotherClass(MainActivity activity) {
// Save instance of main class for future use
mainActivity = activity;
// Call method in MainActivity
mainActivity.startChronometer();
}
}
#8
0
What i have done and it works is create an instance in the MainActivity and getter for that instance:
我所做的和它的工作是在MainActivity和getter中创建一个实例:
public class MainActivity extends AbstractMainActivity {
private static MainActivity mInstanceActivity;
public static MainActivity getmInstanceActivity() {
return mInstanceActivity;
}
And the in the onCreate method just point to that activity:
onCreate方法中只指向这个活动
@Override
protected void onCreate(Bundle savedInstanceState) {
mInstanceActivity = this;
}
And in onDestroy you should set this instance to null:
在onDestroy中,你应该将这个实例设置为null:
@Override
protected void onDestroy() {
super.onDestroy();
mInstanceActivity = null;
}
Later you can call every method in whatever class you want:
之后你可以调用任何你想要的类中的每个方法:
MainActivity.getmInstanceActivity().yourMethod();
#9
-1
Simply, You can make this method static as below:
简单地说,您可以使这个方法保持静态,如下所示:
public static void startChronometer(){
mChronometer.start();
showElapsedTime();
}
you can call this function in other class as below:
可以在其他类中调用此函数,如下所示:
MainActivity.startChronometer();
#10
-2
You can make this method static.
您可以使这个方法静态。
public static void startChronometer(){
mChronometer.start();
showElapsedTime();
}
you can call this function in other class as below:
可以在其他类中调用此函数,如下所示:
MainActivity.startChronometer();
OR
或
You can make an object of the main class in second class like,
你可以在第二类中创建一个主类的对象,
MainActivity mActivity = new MainActivity();
mActivity.startChronometer();
#1
13
You can easily call a method from any Fragment inside your Activity by just doing a cast like this:
您可以通过以下方式从活动中的任何片段调用方法:
((MainActivity)getActivity()).startChronometer();
Hope this helps!
希望这可以帮助!
#2
8
Initialize it first
首先初始化
MainActivity mActivity= new MainActivity();
Then you can continue
然后你可以继续
mActivity.startChronometer();
#3
8
I would suggest, one should not make object of an Activity type class.
我的建议是,不应该创建活动类型类的对象。
MainActivity mActivity = new MainActivity(); // BIG NO TO THIS.
All Activities in Android must go through the Activity lifecycle so that they have a valid context attached to them.
Android中的所有活动都必须经过活动生命周期,以便它们有一个有效的上下文。
By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.
通过将活动视为一个普通的Java类,您将得到一个空上下文。由于活动中的大多数方法在其上下文中被调用,您将得到一个空指针异常,这就是为什么您的应用程序会崩溃。
Instead, move all such methods which need to be called from other classes into a Utility class which accepts a valid context in its constructor, and then use that context in the methods to do the work.
相反,将需要从其他类调用的所有此类方法移动到实用程序类中,该实用程序类在其构造函数中接受有效上下文,然后在方法中使用该上下文来完成工作。
#4
4
MainActivity.java
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static MainActivity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instance = this;
}
public static MainActivity getInstance() {
return instance;
}
public void myMethod() {
// do something...
}
)
AnotherClass.java
AnotherClass.java
public Class AnotherClass() {
// call this method
MainActivity.getInstance().myMethod();
}
#5
3
But an error occurred which says java.lang.NullPointerException.
但是出现了一个错误,它说java.lang.NullPointerException。
Thats because, you never initialized your MainActivity. you should initialize your object before you call its methods.
这是因为,您从未初始化主活动。在调用它的方法之前,应该初始化对象。
MainActivity mActivity = new MainActivity();//make sure that you pass the appropriate arguments if you have an args constructor
mActivity.startChronometer();
#6
2
Use this code in sub Fragment of MainActivity
to call the method on it.
在MainActivity的子片段中使用此代码调用该方法。
((MainActivity) getActivity()).startChronometer();
#7
0
You have to pass instance of MainActivity into another class, then you can call everything public (in MainActivity) from everywhere.
您必须将MainActivity实例传递到另一个类中,然后您可以从任何地方调用everything public(在MainActivity中)。
MainActivity.java
MainActivity.java
public class MainActivity extends AppCompatActivity {
// Instance of AnotherClass for future use
private AnotherClass anotherClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Create new instance of AnotherClass and
// pass instance of MainActivity by "this"
anotherClass = new AnotherClass(this);
}
// Method you want to call from another class
public void startChronometer(){
...
}
}
AnotherClass.java
AnotherClass.java
public class AnotherClass {
// Main class instance
private MainActivity mainActivity;
// Constructor
public AnotherClass(MainActivity activity) {
// Save instance of main class for future use
mainActivity = activity;
// Call method in MainActivity
mainActivity.startChronometer();
}
}
#8
0
What i have done and it works is create an instance in the MainActivity and getter for that instance:
我所做的和它的工作是在MainActivity和getter中创建一个实例:
public class MainActivity extends AbstractMainActivity {
private static MainActivity mInstanceActivity;
public static MainActivity getmInstanceActivity() {
return mInstanceActivity;
}
And the in the onCreate method just point to that activity:
onCreate方法中只指向这个活动
@Override
protected void onCreate(Bundle savedInstanceState) {
mInstanceActivity = this;
}
And in onDestroy you should set this instance to null:
在onDestroy中,你应该将这个实例设置为null:
@Override
protected void onDestroy() {
super.onDestroy();
mInstanceActivity = null;
}
Later you can call every method in whatever class you want:
之后你可以调用任何你想要的类中的每个方法:
MainActivity.getmInstanceActivity().yourMethod();
#9
-1
Simply, You can make this method static as below:
简单地说,您可以使这个方法保持静态,如下所示:
public static void startChronometer(){
mChronometer.start();
showElapsedTime();
}
you can call this function in other class as below:
可以在其他类中调用此函数,如下所示:
MainActivity.startChronometer();
#10
-2
You can make this method static.
您可以使这个方法静态。
public static void startChronometer(){
mChronometer.start();
showElapsedTime();
}
you can call this function in other class as below:
可以在其他类中调用此函数,如下所示:
MainActivity.startChronometer();
OR
或
You can make an object of the main class in second class like,
你可以在第二类中创建一个主类的对象,
MainActivity mActivity = new MainActivity();
mActivity.startChronometer();