Java 自定义android.app.Application未触发onCreate事件
我正在从android.app.application派生一个自定义应用程序,无法触发其onCreate事件。下面是实现Java 自定义android.app.Application未触发onCreate事件,java,android,events,Java,Android,Events,我正在从android.app.application派生一个自定义应用程序,无法触发其onCreate事件。下面是实现 import android.app.Application; public class MyApplication extends Application { public MyApplication() { super(); } @Override public void onCreate() { su
import android.app.Application;
public class MyApplication extends Application {
public MyApplication() {
super();
}
@Override
public void onCreate() {
super.onCreate();
}
}
下面是我如何使用它:
MyApplication ctrl = new MyApplication();
不要构造它,从
上下文中获取它
例如,从活动
:
MyApplication ctrl = (MyApplication)getApplicationContext();
更多信息:
文档中说,onCreate()
是
在应用程序启动时,在任何其他应用程序之前调用
对象已创建
在您的AndroidManifest.xml
<application
android:name="MyApplication"
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name">
</application>
然后您的onCreate()
将被解雇。您实际上没有使用new
操作符创建活动的实例。相反,你开始这样的意图:
Intent start = new Intent(context, Classname.class);
context.startActivity(start);
public static MyApplication getApp() {
return mInstance;
}
使用new
操作符创建对象时,将永远不会调用onCreate
[编辑]使用new
运算符创建应用程序时,也不会调用onCreate
[/EDIT]
[EDIT2]您可以创建一个静态方法,该方法返回如下应用程序:
Intent start = new Intent(context, Classname.class);
context.startActivity(start);
public static MyApplication getApp() {
return mInstance;
}
[EDIT2]非常简单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.services">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Services">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Declaring the Service in the manifest file-->
<service
android:name=".MyServiceDemo"
android:foregroundServiceType="dataSync" />
</application>
</manifest>
在AndroidManifest.xml
中,在application
标记中,输入application
子类的名称及其在android:name
属性下的路径
示例:
<application
...
android:name=".models.custom.BaseApplication"
...
> ... </application>
。。。
如Balaji所述,即使在应用程序标记下提到类名,您仍然面临问题
<application
android:name="MyApplication"
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"> </application>
然后试试这个:
尝试并禁用即时运行,然后清理项目并重建它,然后再次运行。
这对我有用。
谢谢。我遇到了这个问题,并发现在我的情况下,整个问题都是电话方面的。我重新启动了手机,解决了问题。在清单文件中,确保添加了扩展应用程序的标记I.e类
Tag=android:name=“.MyApplication
ManifestFile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.services">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Services">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Declaring the Service in the manifest file-->
<service
android:name=".MyServiceDemo"
android:foregroundServiceType="dataSync" />
</application>
</manifest>
ctrl.onCreate();
将为您执行此操作…是的,确实如此。但这不应该自动触发吗?我可以调用onCreate()在构造函数中也是如此,但我认为这不是最优雅的解决方案。你应该调用MyApplication构造函数……你应该在清单xml中指向这个类,Android操作系统应该调用它……onCreate Too以及我应该如何声明从AndroidManifest中的活动调用的应用程序?默认情况下,应用程序标记是al准备好添加了。为什么您需要扩展标准应用程序类…它不起作用。我在AndroidManifest.xml和这个附加的应用程序标记中将主活动定义为一个应用程序。您认为这是正确的吗?@BalajiKhadake我的应用程序类从库应用程序类扩展而来,而onCreate in library没有被触发!必须将类名添加到android:name,如android:name=“your.package.com.MyApplication“这不是活动,这是应用程序操作,我的错。”。但这也适用于应用程序。如果使用新运算符创建应用程序,则不会调用onCreate。是,我这样做了,但我需要能够让对象实例能够设置对象属性并调用其方法。您可以创建一个静态方法来返回此应用程序的单例。您不能从静态方法返回此值-\对我也不起作用。实际上,应用程序在这一行崩溃;谢谢这里的问题是,我还应该在AndroidManifest.xml上声明该应用程序,但根据Balaji的回复和评论,我不能在那里定义2个应用程序。你能想出解决这个问题的办法吗?