java 自定义 android.app.Application 不触发 onCreate 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6858158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
custom android.app.Application not firing onCreate event
提问by Narcís Calvet
I'm deriving a custom application from android.app.Application and I can't get its onCreate event being fired. Here's the implementation
我正在从 android.app.Application 派生一个自定义应用程序,但我无法触发它的 onCreate 事件。这是实现
import android.app.Application;
public class MyApplication extends Application {
public MyApplication() {
super();
}
@Override
public void onCreate() {
super.onCreate();
}
}
And here's how I'm using it:
这是我如何使用它:
MyApplication ctrl = new MyApplication();
回答by Balaji Khadake
Add following in your AndroidManifest.xml
在您的 AndroidManifest.xml
<application
android:name="MyApplication"
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name">
</application>
then your onCreate()
will get fired.
那么你的onCreate()
遗嘱就会被解雇。
回答by Brad
I had this issue and found that in my case that the whole issue was phone side. I rebooted the phone and that fixed the issue.
我遇到了这个问题,并发现在我的情况下,整个问题都在电话方面。我重新启动了手机并解决了问题。
回答by Barakuda
Very simple
很简单的
In your AndroidManifest.xml
, within the application
tag enter the name of your Application
sub-class with it's path under the android:name
attribute.
在您的AndroidManifest.xml
, 在application
标签内输入您的Application
子类的名称及其android:name
属性下的路径。
Example:
例子:
<application
...
android:name=".models.custom.BaseApplication"
...
> ... </application>
回答by pawelzieba
Don't construct it, get it from Context
.
不要构建它,从Context
.
For example from Activity
:
例如来自Activity
:
MyApplication ctrl = (MyApplication)getApplicationContext();
More info: Context.getApplicationContext()
更多信息: Context.getApplicationContext()
Documentation says that onCreate()
is
文档说onCreate()
是
Called when the application is starting, before any other application objects have been created
在应用程序启动时调用,在创建任何其他应用程序对象之前
回答by keyboardsurfer
You don't actually create instances of your Activities with the new
operator. Instead you start an Intent like this:
您实际上并没有使用new
操作员创建您的活动的实例。相反,您可以像这样启动 Intent:
Intent start = new Intent(context, Classname.class);
context.startActivity(start);
When creating an object with the new
operator, then onCreate
never will be called.
当使用new
操作符创建对象时,onCreate
永远不会被调用。
[EDIT] When creating Applications with the new
operator onCreate
won't be called either[/EDIT]
[编辑] 使用new
运算符创建应用程序时onCreate
也不会被调用[/编辑]
[EDIT2] You could create a static method that returns the application like this:
[EDIT2] 您可以创建一个静态方法来返回这样的应用程序:
public static MyApplication getApp() {
return mInstance;
}
[/EDIT2]
[/编辑2]
回答by Naveen Kumar
As Balaji Mentioned if your still facing issue even after mentioning class name under application tag
正如 Balaji 所提到的,如果您在应用程序标签下提到类名后仍然面临问题
<application
android:name="MyApplication"
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"> </application>
Then try this:
然后试试这个:
Try and disabling Instant Runand then clean project and Rebuild it and then run again. It worked for me. Thanks.
尝试禁用 Instant Run,然后清理项目并重建它,然后再次运行。它对我有用。谢谢。