Skip to content Skip to sidebar Skip to footer

Having A Classcast Exception In Android While Using Myapplication Class

What i am doing:: I am trying to use Myapplication class to send the data to next activity Problem i am facing:: Having class cast exception BLD_IndividualListOfItems_Starters.jav

Solution 1:

Move the attribute

android:name="com.android.app.MyApplication"

to the first application element in the manifest and delete the second application element.

Btw. is your MyApplication class really in the package com.android.app?

EDIT: you use a different package, so the line should be

android:name="com.example.multitabcheckboxselection.MyApplication"

EDIT2+3: you have put it as an activity now. remove the activity, the attribute must go into the application tag.

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.multitabcheckboxselection"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="18" /><uses-permissionandroid:name="android.permission.INTERNET" /><applicationandroid:name="com.example.multitabcheckboxselection.MyApplication"android:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.multitabcheckboxselection.BreakfastLunchDinnerIndividualListOfItems"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Starters" /><activityandroid:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_MainCourse" /><activityandroid:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_SideCourse" /><activityandroid:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Others" /><activityandroid:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Desert" /><activityandroid:name="com.example.multitabcheckboxselection.ResultActivity" /></application></manifest>

Solution 2:

Normally one would do like this

classMyApplicationextendsApplication {

@OverridepublicvoidonCreate() {
     super.onCreate();
     mInstance = this;
}

staticpublicMyApplicationgetInstance() { return mInstance; }

staticprivateMyApplication mInstance;

}

Then from elsewhere

MyApplicationmyApp= MyApplication.getInstance();

Solution 3:

Change

mApplication = (MyApplication)getApplication();

To

mApplication  = ((MyApplication) getApplicationContext());

And in Manifest.xml define android:name="MyApplication"

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name="MyApplication"  >

Edit

Remove this

<applicationandroid:name="com.android.app.MyApplication"android:icon="@drawable/ic_launcher"android:label="@string/app_name" ></application>

Each application will have only one <application> Tag

Solution 4:

Change to

mApplication = (MyApplication)getApplicationContext();

Edit:

Delete the second application tag and move this to the first

   android:name="com.android.app.MyApplication"

Edit:

publicclassMyApplicationextendsApplication {
privatestaticMyApplication singleton;

publicMyApplicationgetInstance(){
    return singleton;
}
    publicvoidonCreate() {
    super.onCreate();
    singleton = this;
} 
   // other methods
}

Then in Activity

mApplication = MyApplication.getInstance();

Example:

publicclassMainextendsActivity{

ArrayList<HashMap<String,String>> arraylist = newArrayList<HashMap<String,String>>();
MyApplication mapp;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mapp = MyApplication.getInstance();
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText("My String from Application class is"+mapp.hello);
    for(int i=0;i<10;i++)
    {
            HashMap<String,String> map = newHashMap<String,String>();
            map.put("key", "value"+i);
            arraylist.add(map);

    }
    mapp.setArrayListMapData(arraylist);
    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(newOnClickListener()
    {

        @OverridepublicvoidonClick(View v) {
            // TODO Auto-generated method stubstartActivity(newIntent(Main.this,MainActivity.class));
        }

    });

}

main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="51dp"android:text="TextView" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:text="Button" /></RelativeLayout>

MainActivity

publicclassMainActivityextendsListActivity
{

    ListView lstView; 

    ArrayList<HashMap<String,String>> arraylist;
    @OverridepublicvoidonCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
        MyApplication mApplication = MyApplication.getInstance();
        Log.i("................",""+mApplication.hello);
        lstView = getListView();
        lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);    
        arraylist= mApplication.getArrayListMapData();
        String[] from = { "key" };
        int[] to = { R.id.textView1 };
        SimpleAdapter adapter= newSimpleAdapter(this, arraylist,R.layout.row, from, to);
        setListAdapter(adapter);

    }   

}

MyApplication

publicclassMyApplicationextendsApplication {

    ArrayList<HashMap<String, String>> arraylist;
    privatestaticMyApplication instance = null;  
    publicString hello= "Hello global Application";
    @OverridepublicvoidonCreate() {

        super.onCreate();
    }
    publicstaticMyApplicationgetInstance() {
        if(instance == null) {
           instance = newMyApplication();
        }
        return instance;
     }
    publicvoidsetArrayListMapData(ArrayList<HashMap<String, String>> setData)
    {
        arraylist = setData;
    }

    publicArrayList<HashMap<String, String>> getArrayListMapData()
    {
        return arraylist;
    }
}

Manifest file

<applicationandroid:allowBackup="true"android:name="com.example.testlistactivity.MyApplication"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.testlistactivity.Main"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name="com.example.testlistactivity.MainActivity"android:label="@string/app_name" ></activity></application>

Snap1

enter image description here

Snap2

enter image description here

Post a Comment for "Having A Classcast Exception In Android While Using Myapplication Class"