ClassCastException in calling methods added class - java

Sorry if this one is a stupid question.
I am trying to do this Android global variable
I added a new class but I cannot call the methods inside the class. I get:
inconvertible types; cannot cast 'android.app.Application' to 'com.app.android.appname.classname'
here is the code inside the new class:
public class GlobalVars extends Application {
private static int lvl;
public int getLvl() {
return lvl;
}
public void setLvl(int lvl) {
lvl = this.lvl;
}
}
my manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.android.guessinggame">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
android:screenOrientation="sensorLandscape"
<activity android:name=".MainActivity">
android:screenOrientation="sensorLandscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".enterword"></activity>
<activity android:name=".player1turn"></activity>
<activity android:name=".aiturn"></activity>
<activity android:name=".chooselevel"></activity>
<application android:name=".GlobalVars"/>
</application>
</manifest>
then in the activity:
protected void select1 (View view) {
((GlobalVars) this.getApplication()).setLvl(1); <-------- error
Intent intent = new Intent(this, enterword.class);
startActivity(intent);
}
this produces:
inconvertible types; cannot cast 'android.app.Application' to 'com.app.android.guessinggame.GlobalVars'
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.app.android.guessinggame.GlobalVars
Solved
in the manifest, the name must be defined. not add. changed it to:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.android.guessinggame">
<application
android:name=".GlobalVars"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
android:screenOrientation="sensorLandscape"
<activity android:name=".MainActivity">
android:screenOrientation="sensorLandscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".enterword"></activity>
<activity android:name=".player1turn"></activity>
<activity android:name=".aiturn"></activity>
<activity android:name=".chooselevel"></activity>
</application>
</manifest>
now it works.

If you want to make GlobalVars your Application class in Android you have to make it extend android.app.Application.
You also have to declare this in the Manifest.
On a side note, there is only one instance of Application. You may not need to make lvl static.
public class GlobalVars extends Application{
private static int lvl;
public int getLvl() {
return lvl;
}
public void setLvl(int lvl) {
lvl = this.lvl;
}
}
In the manifest you have to have:
<application
android:name=".GlobalVars"
...
</application>
In your activity, in onCreate() for example:
((GlobalVars) getApplication()).getLvl();

Related

android.app.Application cannot be cast to MainActivity

I get this error when I'm trying to start an intent from my wear to my mobile. When I press a button on my watch there's a message send to the listenerservice. And I'm trying to start a method from my listenerService to my MainActivity but I'm getting the error:
java.lang.ClassCastException: android.app.Application cannot be cast to com.my_emergency.samdesmedt.my_emergency.MainActivity
at com.my_emergency.samdesmedt.my_emergency.ListenerServiceFromWear.onMessageReceived(ListenerServiceFromWear.java:28)
This is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my_emergency.samdesmedt.my_emergency">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AddActivity">
</activity>
<activity
android:name=".SettingsActivity">
</activity>
<activity
android:name=".EditSettingsActivity">
</activity>
<activity
android:name=".EditActivity" >
</activity>
<service android:name=".ListenerServiceFromWear">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER"/>
</intent-filter>
</service>
</application>
</manifest>
This is my ListenerServiceFromWear.Java
#Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
if (messageEvent.getPath().equals(WEARPATH)) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("isEmergency", true);
startActivity(intent);
}
}
This is the code in my mainActivity
Intent intent = getIntent();
boolean isEmergency = intent.getBooleanExtra("isEmergency", false);
if(isEmergency){
startEmergencyMode(message);
}
Intent intent = new Intent(getBaseContext(), MainActivity.class);
fixed it

Android: start activity on boot complete

I have tried this on a few android phones with no luck (i did start the app once).
This was mostly generated from android studio 1.3.2 and from some questions like this and that and the other.
Perhaps someone can point out what i am doing wrong.
Thanks
edit: moved receiver inside app, but no joy.
edit2: added missing permissions for receiver. now seems to work on a nexus 4. but not on the kindle fire or the at&t tablets. although it seems to come right away if i press the circle icon on the azpen.
edit3: seems to work sometimes on the fires now.
package acme.startup;
import android.content.*;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
}
package acme.startup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Every Component must be declared inside application tag into manifest. Here you declared receiver outside of application tag. Read Structure of the Manifest File.
Correct manifest wil be
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
You have to add permission to manifest file. Add this before your <application> tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Android service turns red

I want to add a service to my manifest but it gives a warning in.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aura_apps.tygo_asbroek.intentexample" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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=".SecondActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".TygoIntentService"
>
</service>
</application>
Here is the Java code:
package com.aura_apps.tygo_asbroek.intentexample;
public class TygoIntentService extends IntentService {
private static final String TAG = "com.bluelionapps.intentexample";
public TygoIntentService(String name) {
super("TygoIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
//This is what the service does
Toast toast = Toast.makeText(getApplicationContext(), "Service Lauched", Toast.LENGTH_SHORT);
toast.show();
}
}
Here you see my complete Manifest but .TygoIntentService says TygoIntentService.java doesn't have a default constructor of my app. If i run it, it's just working but I want to know if this can give any problems in the future and if it gives one what i need to do.
Remove String name parameter from the constructor:
public TygoIntentService() {
super("TygoIntentService");
}
If you do not use/need the name String there, just remove the constructor, and let the compiler add it behind scenes by default.

Cannot cast from android.app.Application to custom type

I made a class that will hold application wide variables. I made it extend activity and even included it in the manifest but I'm still getting this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blacksmith/com.example.blacksmith.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.blacksmith.GlobalVars
Here is the class:
package com.example.blacksmith;
import android.app.Application;
public class GlobalVars extends Application {
private Boolean DefaultEncrypt;
private String Password;
public GlobalVars(){
DefaultEncrypt = true;
}
public void setDefaultEncrypt(Boolean is){
DefaultEncrypt = is;
}
public Boolean getDefaultEncrypt(){
return DefaultEncrypt;
}
public void setPassword(String pass){
Password = pass;
}
public String getPassword(){
return Password;
}
}
Here is the android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blacksmith"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="Blacksmith"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PasswordCreatorActivity"
android:label="#string/title_activity_password_creator" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
</application>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name=".application.GlobalVars">
</application>
</manifest>
This is how I'm accessing it:
GlobalVars tmp = ((GlobalVars) getApplicationContext());
tmp.setDefaultEncrypt(sharedPref.getBoolean("Default", true));
tmp.setPassword(sharedPref.getString("password", "0x1234"));
Note: I tried this.getApplication() and getApplication() and neither worked
What am I doing wrong and how do I fix it.
Thanks ahead of time.
I am not 100% sure, but you have 2 application stanzas, try moving/replacing the items in :
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name=".application.GlobalVars">
</application>
into your original application stanza :
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name=".application.GlobalVars"
>
<activity
android:name=".MainActivity"
android:label="Blacksmith"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PasswordCreatorActivity"
android:label="#string/title_activity_password_creator" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
</application>

Broadcast receiver not receiving intent and haven't found a solution

1.This is the class that sends broadcast to main Activity:
public class LecturaDatos extends Service {
inetnt = new Intent(this, MainActivity.class);
sendBroadcast(inetnt);
}
2.This is main Activity and receives the broadcast:
class xReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent1) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
System.out.println("received");
}
}
3.Finally this is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.haizea_android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="5" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="xReceiver">
<intent-filter>
<action android:name="com.javacodegeeks.android.A_CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
<service
android:name=".LecturaDatos"
android:enabled="true" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I don't know why but it doesn't work and I have seen similar problems and tried several solutions but they haven't worked for me
you should set action to your broadcasted intent e.g.
inetnt.setAction("com.javacodegeeks.android.A_CUSTOM_INTENT");

Categories

Resources