我試圖通過保存應用程序上下文的位置,所以我做了以下內容:的Android getApplicationContext NullPointerException異常
對myApp類:
進口android.app.Application;
import com.google.android.maps.GeoPoint;
public class myApp extends Application {
private GeoPoint Tunis = new GeoPoint(microdegrees(36.827589),microdegrees(10.171165));
private GeoPoint myLocation=Tunis;
public GeoPoint getMyLocation(){
return myLocation;
}
public void setMyLocation(GeoPoint s){
myLocation = s;
}
private int microdegrees(double value){
return (int)(value*1000000);
}
}
我的清單:
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".myApp">
<uses-library android:name="com.google.android.maps"/>
<activity android:name=".Main"
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>
,在我的主要活動我這樣做:
myApp appState = ((myApp)getApplicationContext());
,這將導致一個NullPointerException當我啓動應用程序:(可以請你告訴我爲什麼會發生這種異常?我認爲在myApp類中,我將MyLocation初始化爲現有的GeoPoint?
個謝謝,
沒有更多的源代碼和完整的堆棧跟蹤,沒有人可以幫助你。 – CommonsWare 2011-01-09 13:36:08