-1

My application is crashing while launching with following error.

05-13 05:55:33.031: E/AndroidRuntime(1022): FATAL EXCEPTION: main
05-13 05:55:33.031: E/AndroidRuntime(1022): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myphonecall/com.example.myphonecall.MyPhoneCall}: java.lang.ClassCastException: com.example.myphonecall.MyPhoneCall cannot be cast to android.app.Activity
05-13 05:55:33.031: E/AndroidRuntime(1022):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at android.os.Looper.loop(Looper.java:137)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at java.lang.reflect.Method.invoke(Method.java:511)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at dalvik.system.NativeStart.main(Native Method)
05-13 05:55:33.031: E/AndroidRuntime(1022): Caused by: java.lang.ClassCastException: com.example.myphonecall.MyPhoneCall cannot be cast to android.app.Activity
05-13 05:55:33.031: E/AndroidRuntime(1022):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-13 05:55:33.031: E/AndroidRuntime(1022):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-13 05:55:33.031: E/AndroidRuntime(1022):     ... 11 more

CODE

MyPhoneCall.java package com.example.myphonecall;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class MyPhoneCall extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
      String state = extras.getString(TelephonyManager.EXTRA_STATE);
      Log.w("MY_DEBUG_TAG", state);
      if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
    String phoneNumber = extras
        .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
    //Log.w("MY_DEBUG_TAG", phoneNumber);
    Toast.makeText(context, "Getting Call from "+phoneNumber, Toast.LENGTH_LONG).show();
      }
    }
  }
} 

AndroidMAnifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myphonecall"
    android:versionCode="1"
    android:versionName="1.0" >

     <uses-sdk android:minSdkVersion="15" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" >
    </uses-permission>

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
       <activity
        android:name=".MyPhoneCall"
        android:label="MyPhoneCall" >
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="MyPhoneReceiver" >
        <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" >
        </action>
        </intent-filter>
    </receiver>
    </application>



</manifest> 
matzone
  • 5,703
  • 3
  • 17
  • 20
  • I think you have your Activity and your receiver reversed because you did define the MyPhoneReceiver but you applied the BroadcastReceiver to the MyPhoneCall class. This class should be defined in the receiver like the the answers show. Which class has your actual activity because I don't see it in your MyPhoneCall class... – the-ginger-geek May 13 '13 at 06:15

5 Answers5

2

Your MyPhoneCall is a BroadcastReceiver And you declare it as an activity. So it gives you error.

05-13 05:55:33.031: E/AndroidRuntime(1022): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myphonecall/com.example.myphonecall.MyPhoneCall}: java.lang.ClassCastException: com.example.myphonecall.MyPhoneCall cannot be cast to android.app.Activity

Declare it as following way.

<receiver android:name=".MyPhoneCall" >
        <intent-filter>
            <action ...... />
            <action ..... />
        </intent-filter>
    </receiver>
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
1

MyPhoneCall.java is a BroadcastReceiver. It is not an Activity. But in your AndroidManifest.xml you have declared it as an Activity. Therefore you receive a ClassCastException.

Rajitha Siriwardena
  • 2,749
  • 1
  • 18
  • 22
0

Somewhere you're trying to launch that class as an activity. It isn't- its a receiver. An activity is a workflow with a UI that can take user input and hold the foreground of the phone. A BroadcastReceiver is a short lived piece of code that responds to an event and then goes away. It has no UI, does not interact with the user, and quickly exits. Based on this and the previous question I don't think you understand them.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

In the Manifest you have declared MyPhoneCall as activity . But in the code you have extended MyPhoneCall class with broadcast reciever.

Kasturiwale
  • 85
  • 2
  • 6
0

Beacuse MyPhoneCall class is not extends the Activity class That why this error. Do not put in activity block . Just register in Receiver block in menifest

Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50