Android - 应用程序崩溃“java.lang.ClassCastException”

时间:2017-09-15 10:04:28

标签: java android google-analytics classcastexception

我正在使用tutorial provided by Google在我的应用中实施 Google Analytics ,但我可能做错了导致应用程序崩溃java.lang.ClassCastException

这是谷歌提供的:

// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();

这是我所做的更改,因为我使用的是片段

// This is where I get the error
AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();
mTracker = application.getDefaultTracker();

更新:错误发生在此行:

AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();

这是我的LogCat

FATAL EXCEPTION: main
Process: com.incorp.labs.appname, PID: 14095
java.lang.ClassCastException: android.app.Application cannot be cast to com.incorp.labs.appname.Helper.AnalyticsTracker
  at com.incorp.labs.appname.OneFragment.onCreateView(OneFragment.java:126)

更新2:这是清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.incorp.labs.appname">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.vending.BILLING" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/newlogops"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/MyMaterialTheme">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".Splash"
        android:screenOrientation="portrait" />
    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".OneFragment"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".TwoFragment"
        android:screenOrientation="portrait" />
    <activity
        android:name=".Feedback"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".FourFragment"
        android:screenOrientation="portrait" />
    <activity
        android:name=".SplashTimer"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

    <service android:name=".FirebaseMessagingService">
        <intent-filter>                     
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
             
        </intent-filter>
    </service>

    <activity android:name=".AboutActivity"></activity>
</application>

3 个答案:

答案 0 :(得分:4)

只需改变一下: -

AnalyticsTracker application = (AnalyticsTracker) getContext().getApplicationContext();

到此: -

AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();

这是我们中最好的人发生的明显错误之一!

答案 1 :(得分:2)

除了实现extends Application类之外,您还需要告诉Android系统将哪个类用作Application类。为此,请在清单中设置应用程序类的名称:

<application
        android:name="your.package.AnalyticsApplication"

答案 2 :(得分:1)

我认为您正在尝试将Context对象(getApplicationContext())用作应用程序对象。

试试这个:编辑

 public static function getHmacParameters($method, $path, $content, 
 $username, $salt) { 

 $version = "HTTP/1.1" ; 

 $requestLine = strtoupper($method) . " " . $path . " " . $version; 
 $dateString = gmdate('D, d M Y H:i:s T'); 
 $digest = base64_encode(hash("sha256", $content, true)); 

 $signing_headers = [ 
    'date' => $dateString, 
    'request-line' => $requestLine, 
    'digest' => $digest 
 ]; 

 $signing_string = ""; 
 $headers = ""; 
 foreach ($signing_headers as $key => $value) { 
    if ($key != "request-line") { 
        $signing_string .= $key . ":" . " "; 
    } 
    $signing_string .= $value . "\n"; 
    $headers .= $key . " "; 
 } 
 $signing_string = rtrim($signing_string, "\n"); 
 $headers = rtrim($headers, " "); 

 $hmacHash = hash_hmac("sha1", $signing_string, $salt, true); 

 $signature = base64_encode($hmacHash); 
 $authorization = "hmac username=\"$username\", algorithm=\"hmac-sha1\", 
 headers=\"$headers\", signature=\"$signature\""; 

 return [ 
    'dateString' => $dateString, 
    'digest' => $digest, 
    'authorization' => $authorization 
 ]; 

} 

从您的片段中,您可以调用包含该片段的活动。当您获得活动时,您可以使用示例中的getApplication()函数。

我希望它有所帮助。

编辑2

如以下答案所示:来自CommonsWare用户的https://stackoverflow.com/a/35905601/4336354

  

您的AndroidManifest.xml文件不包含您的自定义应用程序   子类,因此Android使用默认的   (android.app.Application)。

     

添加一个android:name =&#34; auc.games2.Analytics.AnalyticsApplication&#34;   属性为你的元素。