Android - App crashing with "java.lang.ClassCastException" - java

I am using a tutorial provided by Google to implement Analytics in my app but something I possibly did wrong that causes the app to crash with java.lang.ClassCastException
This is what Google provided:
// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
This is the changes I made because I am using a Fragment
// This is where I get the error
AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();
mTracker = application.getDefaultTracker();
UPDATE : The error happens at this line:
AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();
This is my 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)
UPDATE 2 : This is the Manifest file
<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>

Just change this :-
AnalyticsTracker application = (AnalyticsTracker) getContext().getApplicationContext();
to this :-
AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();
It's one of those obvious errors that happen with the best of us!

In addition to implementing the extends Application class, you need to tell Android system which class to use as an Application class. To do so, set the name of your application class in manifest:
<application
android:name="your.package.AnalyticsApplication"

I think you're trying to use a Context object (getApplicationContext()) as an application object.
Try this: EDIT
AnalyticsApplication application = (AnalyticsApplication) getActivity().getApplication();
From your fragment you can call the activity that contains the fragment. When you get the activity, you can use the getApplication() function as in the example.
I hope it helps.
EDIT 2
As seen in this answer: https://stackoverflow.com/a/35905601/4336354 from CommonsWare user.
Your AndroidManifest.xml file does not contain your custom Application
subclass, so Android is using the default one
(android.app.Application).
Add an android:name="auc.games2.Analytics.AnalyticsApplication"
attribute to your element.

Related

My android app is not rotating. Where could the problem be?

Rotating can't seem to work in my app. I can't find where the problem is. If someone could help with that. At first it was working, but then I don't what changed in the code that made it stop rotating.If any classes other than the android manifest may contain the error please let me know in order to upload it Thank you.
Here is my AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.how">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the "MyLocation" functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher_foreground"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_foreground"
android:supportsRtl="true"
android:theme="#style/Theme.HOW">
<activity android:name=".UploadID"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.UploadID"/>
<activity android:name=".Register" />
<activity android:name=".Login" />
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
<provider
android:authorities="com.example.how.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"/>
</provider>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
To use the user specified rotation, use this code:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
Please remove android:screenOrientation="portrait" in the manifest file otherwise add sensor instead of portrait.
Or in the whole Activity add android:screenOrientation="sensor" in the manifest

Unable to remove activity from stack,

I am starting an activity from service. ,
In this i set two flags one is
Intent.FLAG_ACTIVITY_NEW_TASK
because i am running activity from service.its new
Intent.FLAG_ACTIVITY_NO_HISTORY is set to new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished.
Here is the code which i call from service to open activity
public static void open(Context context){
Intent intent=new Intent(context,PopupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(intent);
}
and i have cross button in my activity to destroy activity , when i press cross button activity should remove from stack,but it remain in stack and when press home button to view it, activity recreated in stack.for example "oncreate" is again called
here is my code.
#OnClick(R.id.flClose) void closeWindow(){
finish();
}
Here is my manifest activity code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackagename">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name=".app.MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DashboardActivity"
android:screenOrientation="portrait" />
<activity
android:name=".SearchUserActivity"
android:screenOrientation="portrait" />
<activity
android:name=".TermsPolicyActivity"
android:screenOrientation="portrait" />
<activity
android:name=".UserDetailActivity"
android:screenOrientation="portrait" />
<activity
android:name=".DocumentViewerActivity"
android:screenOrientation="portrait" />
<activity
android:noHistory="true"
android:name=".PopupEndActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="#style/Theme.AppCompat.Transparent.NoActionBar" />
<activity
android:name=".BlockActivity"
android:theme="#style/Theme.AppCompat.Transparent.NoActionBar" />
<activity
android:name=".BlockSettingsActivity"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|keyboardHidden"
/>
<service
android:name=".service.SyncDataService"
android:enabled="true"
android:exported="false" />
</application>
</manifest>
Try using finishAffinity() instead of finish()
Add the following flags instead of setflags to open activity
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
And on close button call the function below
private void endTask() {
if (Build.VERSION.SDK_INT >= 21) {
finishAndRemoveTask();
} else {
finish();
}
}
You can get this behaviour by setting the android:noHistory attribute to "true" in the relevant entries in your AndroidManifest.xml file.
For example:
<activity
android:name=".YourActivity"
android:noHistory="true" />
I solved My problem by Adding flag Intent.FLAG_ACTIVITY_NEW_DOCUMENT This flag is used to open a document into a new task rooted at the activity launched
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
In manifest i have noHistory
android:noHistory="true"

startActivity crashes if I do not open the activity from another path

So I have 2 ways an activity can be opened. One is from the activity flow of:
Main > Tracks > Day > Topic > TrackSelect > TrackInfo
and the other is:
Main > MySchedule > TrackInfo
If I try to get TrackInfo to open up via the second path, it crashes the app.
However, If I go from the first path, then all the way back to the main, then through the second path, it works perfectly. Is there something weird going on?
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fayko.conference_app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Conference-App"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".mainSelection"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".topicScreen"/>
<activity
android:name=".myScheduleScreen" />
<activity android:name=".trackSelection" />
<activity android:name=".mainScreen" />
<activity android:name=".daySelection" />
<activity android:name=".trackInfoScreen" />
<activity android:name=".mapChoose" />
<activity android:name=".sponsorScreen" />
<activity android:name=".committeeScreen" />
<activity android:name=".welcomeScreen"></activity>
</application>
</manifest>
Code from TrackSelect > Track Info:
Intent intent = new Intent(trackSelection.this,trackInfoScreen.class);
startActivity(intent);
Code from MySchedule > TrackInfo:
Intent intent = new Intent(myScheduleScreen.this,trackInfoScreen.class);
startActivity(intent);
I appreciate any help you guys can give me.
It turns out that the issue I was having was due to some internal code. Even thought he stacktrace was saying things about socket lost for the debugger.

Convert Android Widget to an App with a Widget

I have an Android widget (it is shown in the list of widgets on the phone, and is not shown in the list of apps). It has a button which launches a pop-up activity.
Now I would like to change the widget so, that it would be both an app (which will be shown in the list of apps on the phone) which will start from that pop-up activity, and a widget (the same as before) which will launch the app (which will consist of that pop-up activity, but will be displayed and treated like an app).
What changes should I make in my manifest file to make the changes described above?
Below is my manifest file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Light.NoTitleBar" >
<receiver android:name="myapp.myapp.MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/demo_widget_provider" />
</receiver>
<receiver
android:name="myapp.myapp.MyWidgetIntentReceiver"
android:label="widgetBroadcastReceiver" >
<intent-filter>
<action android:name="GET_HELP" />
<action android:name="CHANGE_PICTURE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/demo_widget_provider" />
</receiver>
<activity
android:name="myapp.myapp.MainActivity"
android:label="#string/title_activity_main" >
</activity>
<activity
android:name="myapp.myapp.CardsChoice"
android:label="#string/title_activity_choice" >
</activity>
</application>
The solution was rather simple: I just needed to add a Launcher Activity to the app.
This operation consists of 2 steps:
Adding activity definition as a launcher activity to the Manifest
Example:
<activity
android:name="package.package"
android:label="#string/title_activity_main"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Adding code of the activity (and the layout) to the app.
It is about an class MainActivity and a layout file activity_main.xml

Activity permission Denial

I've encountered a strange permission denial in my Android app, here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="test.mymax"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/splash"
android:label="#string/app_name" android:debuggable="true">
<activity
android:name=".Test_mymaxActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="A_Info"></activity>
<activity android:name="A_Info_Refresh"></activity>
<activity android:name="_TagConnector"></activity>
<activity android:name="_SQLconnect"></activity>
<activity android:name="_TagReader"></activity>
<activity android:name="_TagReader_Refresh"></activity>
<activity android:name="Test_mymaxActivity"></activity>
</application>
</manifest>
the error i get is:
ActivityManager: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=test.mymax/.Test_mymaxActivity } from null (pid=17572, uid=2000) requires null
Please help I have no idea on what's wrong
You declared your Test_mymaxActivity twice. Once here
<activity
android:name=".Test_mymaxActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and below a second time
<activity android:name="Test_mymaxActivity"></activity>
Can you post the Intent/Code you are using that is requesting the permission? Or is this immediately on launching your activity?
Are you sure you declared all the permission needed for your code (using internet, detect phone state, etc)
Here's the documentation : Manifest permission - Android dev

Categories