1
我在啓動我的應用程序時遇到ClasscastException,問題出在TaxiTwinApplication app =(TaxiTwinApplication)getApplication();這是我試圖從其它類調用方法中的活性甲LauncherActivity中的ClasscastException
公共類LauncherActivity延伸活動{
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent intent;
TaxiTwinApplication app = (TaxiTwinApplication)getApplication();
app.register();
switch (TaxiTwinApplication.getUserState()) {
case NOT_SUBSCRIBED:
case SUBSCRIBED:
intent = new Intent(this, MainActivity.class);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
break;
case PARTICIPANT:
intent = new Intent(this, MyTaxiTwinActivity.class);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
break;
case OWNER:
intent = new Intent(this, MyTaxiTwinActivity.class);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
break;
default:
intent = new Intent(this, MainActivity.class);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
}
startActivity(intent);
finish();
}
}
公共類TaxiTwinApplication延伸申請{
private static int pendingNotificationsCount = 0;
private static UserState userState = NOT_SUBSCRIBED;
private ServicesManagement servicesManagement;
private LocationListener locationListener;
@Override
public void onCreate() {
super.onCreate();
}
public void unregister() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(locationListener);
}
public void register() {
servicesManagement = new ServicesManagement(this);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
locationUpdate(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 3, this);
}
public void onProviderDisabled(String provider) {
}
};
//every 10 seconds and at least 3 meters
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 3, locationListener);
}
private void locationUpdate(Location location) {
GcmHandler gcmHandler = new GcmHandler(this);
gcmHandler.locationChanged(location);
}
public static int getPendingNotificationsCount() {
return pendingNotificationsCount;
}
public static void setPendingNotificationsCount(int pendingNotifications) {
pendingNotificationsCount = pendingNotifications;
}
public static UserState getUserState() {
return userState;
}
public static void setUserState(UserState userState) {
TaxiTwinApplication.userState = userState;
}
public static void exit(Context context) {
GcmHandler gcmHandler = new GcmHandler(context);
if (userState == UserState.OWNER || userState == UserState.PARTICIPANT) {
gcmHandler.leaveTaxiTwin();
}
gcmHandler.unsubscribe();
TaxiTwinApplication app = (TaxiTwinApplication) context.getApplicationContext();
app.unregister();
DbHelper dbHelper = new DbHelper(context);
dbHelper.deleteTables(dbHelper.getWritableDatabase());
android.os.Process.killProcess(android.os.Process.myPid());
}
}
是否在清單中的'標記中聲明瞭'android:name =「。TaxiTwinApplication」'? –
不,先生,我沒有實際上我是新的應用程序開發,所以我不知道,我現在就嘗試 – kinan
當在清單中使用標記時,不允許元素應用程序 –
kinan