/ / java.lang.RuntimeException: Die Aktivität ComponentInfo {…} kann nicht gestartet werden: java.lang.ClassCastException: android.app.Application kann nicht in android, classcastexception, runtimeexception umgewandelt werden

java.lang.RuntimeException: Aktivität kann nicht gestartet werden ComponentInfo {...}: java.lang.ClassCastException: android.app.Application kann nicht in android, classcastexception, runtimeexception umgewandelt werden

Ich erstelle eine Android App, die Microsoft Hawaii OCR verwendet;

02-08 11:38:38.861: E/AndroidRuntime(4704): FATAL EXCEPTION: main
02-08 11:38:38.861: E/AndroidRuntime(4704): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobile/com.example.mobile.RecognitionActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to microsoft.hawaii.sampleappbase.HawaiiBaseApplication
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.os.Looper.loop(Looper.java:137)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.app.ActivityThread.main(ActivityThread.java:4476)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at java.lang.reflect.Method.invoke(Method.java:511)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at dalvik.system.NativeStart.main(Native Method)
02-08 11:38:38.861: E/AndroidRuntime(4704): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to microsoft.hawaii.sampleappbase.HawaiiBaseApplication
02-08 11:38:38.861: E/AndroidRuntime(4704):     at microsoft.hawaii.sampleappbase.HawaiiBaseAuthActivity.getBaseApplication(HawaiiBaseAuthActivity.java:204)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at microsoft.hawaii.sampleappbase.HawaiiBaseAuthActivity.getAuthenticationTypeFromApp(HawaiiBaseAuthActivity.java:117)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at microsoft.hawaii.sampleappbase.HawaiiBaseAuthActivity.onCreate(HawaiiBaseAuthActivity.java:52)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at com.example.mobile.RecognitionActivity.onCreate(RecognitionActivity.java:32)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.app.Activity.performCreate(Activity.java:4636)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
02-08 11:38:38.861: E/AndroidRuntime(4704):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
02-08 11:38:38.861: E/AndroidRuntime(4704):     ... 11 more

Der Fehler bezieht sich auf ein importiertes Projekt, insbesondere auf diesen Code:

package microsoft.hawaii.sampleappbase;

import microsoft.hawaii.hawaiiClientLibraryBase.Identities.ClientIdentity;
import microsoft.hawaii.hawaiiClientLibraryBase.Util.Utility;
import microsoft.hawaii.sampleappbase.HawaiiBaseApplication.AuthenticationType;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Toast;

/**
* Base Activity for Hawaii authentication
*/
public class HawaiiBaseAuthActivity extends Activity {

/**
* Error code for missing authentication type in configuration file
*/
static final int MISSING_NETWORK_CONNECTION = 0;

/**
* Error code for missing authentication value in configuration file
*/
static final int MISSING_AUTHENTICATION_TYPE = 1;

/**
* Error code for missing authentication value in configuration file
*/
static final int MISSING_AUTHENTICATION_VALUES = 2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
this.showDialog(MISSING_NETWORK_CONNECTION);
return;
}

if (!this.shouldAuthenticate()) {
return;
}

AuthenticationType authType = this.getAuthenticationTypeFromApp();
if (authType == AuthenticationType.NOVALUE) {
this.showDialog(MISSING_AUTHENTICATION_TYPE);
return;
}

// create ClientIdentity object based on configured Hawaii client
// credential
ClientIdentity identity = null;
try {
switch (authType) {
case GUID:
String appId = this.getString(R.string.hawaii_GUID_app_ID);
if (Utility.isStringNullOrEmpty(appId)) {
this.showDialog(MISSING_AUTHENTICATION_VALUES);
return;
}

identity = ClientIdentity.createClientIdentity(appId);
break;
case ADM:
String clientId = this.getString(R.string.hawaii_ADM_client_ID);
String clientSecret = this
.getString(R.string.hawaii_ADM_client_secret);
String serviceScope = this
.getString(R.string.hawaii_ADM_service_scope);

if (Utility.isStringNullOrEmpty(clientId)
|| Utility.isStringNullOrEmpty(clientSecret)
|| Utility.isStringNullOrEmpty(serviceScope)) {
this.showDialog(MISSING_AUTHENTICATION_VALUES);
return;
}
identity = ClientIdentity.createClientIdentity(clientId,
clientSecret, serviceScope);
break;
case NOVALUE:
this.showErrorMessage(
"Couldn"t configure client identity for calling Hawaii service",
null);
return;
}
} catch (Exception exception) {
this.showErrorMessage(
"Couldn"t configure client identity for calling Hawaii service",
exception);
}

if (identity != null) {
this.getBaseApplication().setClientIdentity(identity);
} else {
this.showErrorMessage(
"Couldn"t configure client identity for calling Hawaii service",
null);
}
}

/**
*
* getAuthenticationTypeFromApp Return authentication type which assigned by
* application
*
* @return AuthenticationType
*/
protected AuthenticationType getAuthenticationTypeFromApp() {
return this.getBaseApplication().getAuthType();
}

/**
* Return a flag to indicate whether current Activity needs authentication
*
* @return boolean
*/
protected boolean shouldAuthenticate() {
return true;
}

/**
* show ErrorMessage using AlertDialog
*
* @param title
*            title to display on the dialog
* @param exception
*            exception to display on the dialog
*/
protected void showErrorMessage(String title, Throwable exception) {
if (exception != null) {
exception.printStackTrace();
System.out.println(exception.toString());
}

AlertDialog dialog = dialogToShow(title, exception).create();
dialog.setCanceledOnTouchOutside(true);
dialog.show();
}

/**
* show ErrorMessage using AlertDialog
*
* @param title
*            title to display on the dialog
* @param exception
*            exception to display on the dialog
*/
protected void showErrorMessage(AlertDialog.Builder builder) {
if (builder == null) {
return;
}

AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
dialog.show();
}

/**
* show specified message in toast
*
* @param message
*            specified message
*/
protected void showMessage(String message) {
if (!Utility.isStringNullOrEmpty(message)) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

/**
* create a {@link AlertDialog.Builder} to show dialog
*
* @param title
*            title to display on the dialog
* @param exception
*            exception to display on the dialog
* @return AlertDialog.Builder
*/
protected AlertDialog.Builder dialogToShow(String title, Throwable exception) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
if (exception != null) {
builder.setMessage(exception.getLocalizedMessage());
}

builder.setCancelable(true);
return builder;
}

/**
* gets current {@link HawaiiBaseApplication} object
*
* @return HawaiiBaseApplication
*/
protected HawaiiBaseApplication getBaseApplication() {
return (HawaiiBaseApplication) this.getApplication();
}

@Override
protected Dialog onCreateDialog(int id) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setTitle(getString(R.string.configuration_error_title));
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);

String errorMessage = this.getErrorMessage(id);
dialog.setMessage(errorMessage);

final Activity activity = this;
dialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
activity.finish();
}
});

return dialog;
}

protected String getErrorMessage(int id) {
switch (id) {
case MISSING_NETWORK_CONNECTION:
return getString(R.string.error_missing_network_connection);
case MISSING_AUTHENTICATION_TYPE:
return getString(R.string.error_missing_authentication_type);
case MISSING_AUTHENTICATION_VALUES:
return getString(R.string.error_missing_authentication_value);
default:
return null;
}
}
}

Dies ist der Code von HawaiiBaseApplication

/**
* Copyright (c) Microsoft Corporation. All rights reserved.
*/
package microsoft.hawaii.sampleappbase;

import microsoft.hawaii.hawaiiClientLibraryBase.Identities.ClientIdentity;
import android.app.Application;

/**
* Base application class for Hawaii sample applications
*/
public class HawaiiBaseApplication extends Application {

/**
* Hawaii authentication type
*/
private AuthenticationType authType;

/**
* ClientIdentity object
*/
private ClientIdentity identity;

/**
* Enum class to represent Hawaii authentication type
*/
public enum AuthenticationType {
GUID, ADM, NOVALUE;

/**
* Convert to authentication type enum for specified String
*
* @param str
*            specified String
* @return AuthenticationType
*/
public static AuthenticationType convertToAuthType(String str) {
try {
return valueOf(str.toUpperCase());
} catch (Exception ex) {
return NOVALUE;
}
}
}

/**
* Gets current authentication type
*
* @return AuthenticationType
*/
public AuthenticationType getAuthType() {
if (authType == null) {
authType = AuthenticationType
.convertToAuthType(getString(R.string.hawaii_authentication_type));
}

return authType;
}

/**
* Gets current ClientIdentity object
*
* @return ClientIdentity
*/
public ClientIdentity getClientIdentity() {
return this.identity;
}

/**
* Sets current ClientIdentity object
*
* @param identity
*            current ClientIdentity object
*/
public void setClientIdentity(ClientIdentity identity) {
this.identity = identity;
}

}

Kann mir bitte jemand helfen? Ich "stecke in diesem Punkt fest und weiß nicht, was daran falsch ist ..

Ich danke dir sehr!

Antworten:

0 für die Antwort № 1

Tut dein application Element haben ein android:name="microsoft.hawaii.sampleappbase.HawaiiBaseApplication" Attribut in Ihrem AndroidManifest.xml? Das "was wahrscheinlich fehlt".

http://developer.android.com/guide/topics/manifest/application-element.html