Application crash with “android.app.Application cannot be cast to” - android

I'm disovering ORMlite. So, I have this DAO Factory class
public class DtoFactory extends Application {
private SharedPreferences preferences;
private DatabaseHelper databaseHelper = null;
private Dao<ReleveEntity, Integer> releveDAO = null;
#Override
public void onCreate() {
super.onCreate();
preferences = PreferenceManager.getDefaultSharedPreferences(this);
databaseHelper = new DatabaseHelper(this);
}
public SharedPreferences getPreferences() {return preferences;}
public Dao<ReleveEntity, Integer> getReleveDao() throws SQLException, java.sql.SQLException {
if (releveDAO == null) {
releveDAO = databaseHelper.getDao(ReleveEntity.class);
}
return releveDAO;
}
#Override
public void onTerminate() {
super.onTerminate();
if (databaseHelper != null) {
OpenHelperManager.releaseHelper();
databaseHelper = null;
}
}
}
And in the Main, I'll show just this lines where I have the problem :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dtoFactory = (DtoFactory) getApplication();
And the manifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.exercice.ftouzi.ReleveActivity"
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="com.exercice.ormdatabase.DtoFactory" >
</activity>
</application>
The message error is :
E/AndroidRuntime(19672): FATAL EXCEPTION: main
E/AndroidRuntime(19672): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.formation.adapter/com.exercice.ftouzi.ReleveActivity}:
java.lang.ClassCastException: android.app.Application cannot be cast to
com.exercice.ormdatabase.DtoFactory
Can you please help me to resolve this ?

You need to specify your custom Application (i.e., DtoFactory) in the AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="com.exercice.ormdatabase.DtoFactory">
<activity
android:name="com.exercice.ftouzi.ReleveActivity"
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>

Related

ClassCastException being thrown while performing Manual Dependency Injection in Android [duplicate]

I'm disovering ORMlite. So, I have this DAO Factory class
public class DtoFactory extends Application {
private SharedPreferences preferences;
private DatabaseHelper databaseHelper = null;
private Dao<ReleveEntity, Integer> releveDAO = null;
#Override
public void onCreate() {
super.onCreate();
preferences = PreferenceManager.getDefaultSharedPreferences(this);
databaseHelper = new DatabaseHelper(this);
}
public SharedPreferences getPreferences() {return preferences;}
public Dao<ReleveEntity, Integer> getReleveDao() throws SQLException, java.sql.SQLException {
if (releveDAO == null) {
releveDAO = databaseHelper.getDao(ReleveEntity.class);
}
return releveDAO;
}
#Override
public void onTerminate() {
super.onTerminate();
if (databaseHelper != null) {
OpenHelperManager.releaseHelper();
databaseHelper = null;
}
}
}
And in the Main, I'll show just this lines where I have the problem :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dtoFactory = (DtoFactory) getApplication();
And the manifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.exercice.ftouzi.ReleveActivity"
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="com.exercice.ormdatabase.DtoFactory" >
</activity>
</application>
The message error is :
E/AndroidRuntime(19672): FATAL EXCEPTION: main
E/AndroidRuntime(19672): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.formation.adapter/com.exercice.ftouzi.ReleveActivity}:
java.lang.ClassCastException: android.app.Application cannot be cast to
com.exercice.ormdatabase.DtoFactory
Can you please help me to resolve this ?
You need to specify your custom Application (i.e., DtoFactory) in the AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="com.exercice.ormdatabase.DtoFactory">
<activity
android:name="com.exercice.ftouzi.ReleveActivity"
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>

ListFragment does not show title bar in Android Studio

Why title bar not show it in the ListFragment?
I did not set fullscreen or other.Please help me
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".CrimePagerActivity"
android:label="#string/app_name">
</activity>
</application>
CrimeListFragment.java
public class CrimeListFragment extends ListFragment {
private ArrayList<Crime> mCrimes;
private static final String TAG = "CrimeListFragment";
private Crime c;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().setTitle(R.string.crimes_title);
mCrimes = CrimeLab.get(getActivity()).getCrimes();
CrimeAdapter adapter = new CrimeAdapter(mCrimes);
setListAdapter(adapter);
...
}
}
Check, if there isn't for example "AppTheme.NoActionBar" as your app theme at Design, in your fragment xml file.
change the theme of your activity in manifest.
android:theme=""

Unable to find explicit activity class error

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exampl.fitindya"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="#drawable/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>
<!-- Entry for RegisterActivity.class -->
<activity android:name=".RegisterActivity"
android:label="Register New Account"></activity>
</application>
Don't know why this error is appearing and application crashes. please help guys.can there be any problem anywhere else
here is my class file
public class MainActivity extends ActionBarActivity {
Button login_b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
}
Place this code below the register Activity. I guess that should be the error
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Android - MainActivity keeps on instantiating

Im very new to android development so please bear my ignorance.
I created a Splash screen before loading my main activity. Splash is working fine but what causes the problem is the main activity, it keeps on instantiating.
Splash.java
public class Splash extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
final AdController optinController = new AdController(
getApplicationContext(), "SECTION_ID");
final Splash splash = this;
optinController.loadOptin((Activity) splash, "SECTION_ID",
new AdOptinListener() {
public void onAdOptin() {
// once optin process is complete, continue to main app activity
launchMain();
}
}
);
}
public void launchMain() {
finish();
Intent myIntent = new Intent(Splash.this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(myIntent);
}
}
MainActivity.java
public class MainActivity extends Activity {
private AdController myController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myController = new AdController(getApplicationContext(), "SECTION_ID");
myController.loadNotification();
}
}
In the manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I used android:launchMode="singleInstance" but still it keeps on reinstantiating.
Please help. Thanks in advance.
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//|Window.FEATURE_INDETERMINATE_PROGRESS
setContentView(R.layout.splash);
final Timer time = new Timer();
time.schedule(new TimerTask() {
#Override
public void run() {
Intent intent = new Intent(Splash.this,MainActivity.class);
startActivity(intent);
finish();
}
},1000);
}}
It will work , i din get you exactly by instatiating.Also do no use single instance,you are not supposed to require it here.
Yes, following is my snippet of manifest file , just use a timer for your splash screen and move to your main class after specific time.
activity android:name=".Splash"
android:screenOrientation="portrait" android:label="#string/app_name">
i think you have to remove the intent flag Intent.FLAG_ACTIVITY_SINGLE_TOP
In manifest.xml, set category as LAUNCHER which activity you want to launch first and to Other set as DEFAULT as given in below example.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

Illegal access exception while sharing Global data

i want to share the global data across activities and i have followed
this link
.but i am not getting how to declare it in my manifest. i am posting my manifest code, i have tried it in different ways, but still getting the error. please tell me how to resolve it.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:name=".Myapp">
<activity android:name=".AndroidtestActivity"
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>
This is my main activity
public class AndroidtestActivity extends Activity
{
/** Called when the activity is first created. */
public static final String PREFS_NAME = "MyPrefsFile";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et = (EditText) findViewById(R.id.text1);
Myapp appState = ((Myapp)getApplicationContext());
String s= appState.getState();
et.setText(s);
}
}
and my Myapp class is
class Myapp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = "hello world";
}
}
i am getting error in the line
Myapp appState = ((Myapp)getApplicationContext());
illegal access exception ,please tell me how to resolve this problem
please help me with this.
Merge the application tags (i.e. put the android:name in the first one and delete the rest):
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:name=".MyApp">
<activity android:name=".AndroidtestActivity"
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>
Then use getApplication() instead of getApplicationContext().

Categories

Resources