我使用的是tutorial provided by Google實現Analytics(分析)在我的應用程序但有些事我可能做錯了,導致該應用與java.lang.ClassCastException
的Android - 應用程序與「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();
變化
UPDATE:錯誤發生在這一行:
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>
1)https://stackoverflow.com/questions/31312255/error-on-my-fragment-android-app-application-cannot-被鑄到 2)https://stackoverflow.com/questions/24950341/android-google-analytics-integration-error –
根據文檔,這只是在代碼試圖將一個對象轉換爲子類時拋出其中不是一個實例。 AnalyticsApplication和AnalyticsTracker – Lokesh
@Lokesh我改變了類名,但這不應該是個問題吧? –