ClassCastException being thrown while performing Manual Dependency Injection in Android [duplicate] - 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

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=""

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

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>

Why different activities appear as separate application in android

I am creating a Jigsaw Puzzle application in android. I have created two activites, activity_jigsaw.xml and activity_level.xml. One activity is created by default (Displaying Hello World!) which I modified and created a new activity by following these steps:
File -> New -> Other -> Android Activity
But when I install the application these two files (and all other activities of the application) are installed as a separate project. But at the same time they are also interlinked. The Java code of the files as follows:
Jisaw.java file contains:
public class Jigsaw extends Activity {
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jigsaw);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_jigsaw, menu);
return true;
}
public void play(View v)
{
try
{
intent = new Intent(this, Level.class);
startActivity(intent);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Here Play is a function which is called when an image is clicked.
Level.java file contains:
public class Level extends Activity {
Intent intent;
String level = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_level, menu);
return true;
}
public void easy(View v)
{
level = "easy";
intent = new Intent(this, Play.class);
intent.putExtra("level", level);
startActivity(intent);
}
public void medium(View v)
{
level = "medium";
intent = new Intent(this, Play.class);
intent.putExtra("level", level);
startActivity(intent);
}
public void hard(View v)
{
level = "hard";
intent = new Intent(this, Play.class);
intent.putExtra("level", level);
startActivity(intent);
}
}
Functions easy, medium and hard are called when a corresponding image is clicked.
Can somebody please tell me that what I am doing wrong?
Thanks in advance..
Here is the manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.maju.jigsawpuzzle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Jigsaw"
android:label="#string/title_activity_jigsaw" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Level"
android:label="#string/title_activity_level" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Play"
android:label="#string/title_activity_play" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PlayBoard"
android:label="#string/title_activity_play_board" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You probably are defining android.intent.category.LAUNCHER intent category to all the activities in your AndroidManifest.xml, it creates an icon in the app launcher. Activities other than main should not have this intent filter.
Do something like this:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity android:name=".JigSaw" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Level" />
<activity android:name=".Play" />
<activity android:name=".Playboard" />
</application>
EDIT:
As you just posted, you are indeed doing that, just remove the intent filter from other activities.

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