我遇到了我寫的應用程序類的一些問題。我只是想在我的活動中訪問它,但似乎應用程序從未由android系統實例化(因此onCreate從未被調用)。 我唯一的疑問就是如果android可以實例化所有在manifest中聲明的應用程序或者只是一個。android永遠不會調用onCreate我的應用程序類(在清單中註冊)
下面是代碼:
package org.dyndns.bertuz83.test.applicationTst;
import android.app.Application;
public class MyApplication extends Application{
private String prova="";
private static MyApplication instance;
@Override
public void onCreate() {
super.onCreate();
instance= this;
this.prova="ciao!";
}
public static MyApplication getInstance(){
return instance;
}
public String getProva(){
return this.prova;
}
}
和清單的模樣:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="org.dyndns.bertuz83.test.applicationTst.MyApplication">
<activity
android:name="org.dyndns.bertuz83.test.applicationTst.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<application
android:allowBackup="true"
android:name="org.dyndns.bertuz83.test.applicationTst.MyApplication"></application>
</manifest>
正如你所看到的,我跑活動是根據另一個應用程序中聲明,但我認爲第二應用程序本來會被初始化(事實上,如果我添加android:name =「... MyApplication」,我的應用程序就會正確初始化)!我錯了嗎?
爲什麼你申報兩次?編輯:好的,對不起,這只是一個測試。 – 2013-04-20 15:41:26