ClassCastException Android Eclipse

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

ClassCastException Android Eclipse

Postby dewitrydan » Thu Feb 03, 2011 11:46 am

Hey there

I am getting the following errors when trying to run my application: Can you please tell me where I am going wrong :) Oh and also, how can I find out which line in my code the error is occurring? thanks

Code: Select all
02-03 08:01:31.159: ERROR/AndroidRuntime(281): FATAL EXCEPTION: main
02-03 08:01:31.159: ERROR/AndroidRuntime(281): java.lang.RuntimeException: Unable to instantiate application com.android.demo.notepad2.NoteEdit: java.lang.ClassCastException: com.android.demo.notepad2.NoteEdit
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:649)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4232)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.app.ActivityThread.access$3000(ActivityThread.java:125)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.os.Looper.loop(Looper.java:123)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at dalvik.system.NativeStart.main(Native Method)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): Caused by: java.lang.ClassCastException: com.android.demo.notepad2.NoteEdit
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.app.Instrumentation.newApplication(Instrumentation.java:957)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.app.Instrumentation.newApplication(Instrumentation.java:942)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:644)
02-03 08:01:31.159: ERROR/AndroidRuntime(281):     ... 11 more


This is my NoteEdit class:
Code: Select all
package com.android.demo.notepad2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class NoteEdit extends Activity {
    EditText mTitleText;
    EditText mBodyText;
    Long mRowId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_edit);
        setTitle(R.string.edit_note);
        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        Button confirmButton = (Button) findViewById(R.id.confirm);
        mRowId = null;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String title = extras.getString(NotesDbAdapter.KEY_TITLE);
            String body = extras.getString(NotesDbAdapter.KEY_BODY);
            mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);

            if (title != null) {
                mTitleText.setText(title);
            }
            if (body != null) {
                mBodyText.setText(body);
            }
        }

        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Bundle bundle = new Bundle();
                bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText()
                        .toString());
                bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText()
                        .toString());
                if (mRowId != null) {
                    bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
                }
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        });

    }
}


This is the NoteEdit class layout:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title" />
        <EditText
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/body" />
    <EditText
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scrollbars="vertical" />

    <Button
        android:id="@+id/confirm"
        android:text="@string/confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


This is the manifest:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<manifest
   xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.android.demo.notepad2">
   <application
      android:icon="@drawable/icon"
      android:name=".NoteEdit">
      <activity
         android:name=".Notepadv2"
         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=".NoteEdit">
         <intent-filter>
            <action
               android:name="android.intent.action.VIEW" />
            <category
               android:name="android.intent.category.DEFAULT" />
         </intent-filter>
      </activity>
   </application>
</manifest>


thanks
dewitrydan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Sep 06, 2010 12:03 pm

Top

Re: ClassCastException Android Eclipse

Postby visivi » Thu Feb 03, 2011 3:06 pm

I don't see any problems with NoteEdit and the layout it uses.
Does the Activity in which you start work though?
Refering to the Notepadv2 activity mentioned in the manifest file.
visivi
Developer
Developer
 
Posts: 29
Joined: Mon Nov 01, 2010 3:59 pm

Re: ClassCastException Android Eclipse

Postby dewitrydan » Thu Feb 03, 2011 3:17 pm

Thanks
Nothing works (as soon as i launch the app, i get an error "bla bla bla stopped unexpectedly")
thanks for your time
dewitrydan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Sep 06, 2010 12:03 pm

Re: ClassCastException Android Eclipse

Postby visivi » Thu Feb 03, 2011 3:59 pm

Wait, I just noticed something. In your manifest you define NoteEdit as an application:
Code: Select all
<application android:icon="@drawable/icon"  android:name=".NoteEdit">

When it obviously an Activity based on your given class.
That is what causes the error shown in your stackttrace.

EDIT:

Code: Select all
02-03 08:01:31.159: ERROR/AndroidRuntime(281): java.lang.RuntimeException: Unable to instantiate application com.android.demo.notepad2.NoteEdit: java.lang.ClassCastException: com.android.demo.notepad2.NoteEdit


In the manifest when you define your application, you don't refer to the whole app. Only an instance of an application class.
http://developer.android.com/reference/android/app/Application.html
visivi
Developer
Developer
 
Posts: 29
Joined: Mon Nov 01, 2010 3:59 pm

Re: ClassCastException Android Eclipse

Postby dewitrydan » Thu Feb 03, 2011 4:44 pm

thanks allot
works now :)
dewitrydan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Sep 06, 2010 12:03 pm

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 4 guests