2

My code throwing java.lang.ClassCastException exception error, Why this error will get?. I am using com.android.internal.telephony API's. i.e You can find classes I am using here Call.java<http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/com/android/internal/telephony/Call.java>, CallManger.java <http://hi-android.info/src/com/android/internal/telephony/CallManager.java.html> I have created a Subclaas of Call.java like this:

    public class MyCall extends Call{   

        CallManager cm = CallManager.getInstance(); 
        Phone.State state;
        Connection c;
         Phone mDefaultPhone;
         private final ArrayList<Connection> emptyConnections = new ArrayList<Connection>();    
         MyCall ringingCall;     

        @Override
        public List<Connection> getConnections() {          
            state = cm.getState();      
            ringingCall = (MyCall) cm.getRingingCalls();
            System.out.println("**inside getConnections="+ringingCall);     
            if ( ringingCall != null && !ringingCall.isIdle()) {
                System.out.println("**call is not null***");
                return ((MyCall) ringingCall).getConnections();
            }      
            else
            {
                System.out.println("**list is  null***");
                return emptyConnections;
            }       
        }

        @Override
        public Phone getPhone() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void hangup() throws CallStateException {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean isMultiparty() {
            // TODO Auto-generated method stub
            return false;
        }

        public Connection
        getEarliestConnection() {
            System.out.println("inside EarliestConnection"); 
            List l;
            long time = Long.MAX_VALUE;
            Connection c;
            Connection earliest = null;

            l = getConnections();

            if (l == null) {
                return null;
            }else if ( l.size() == 0)
            {
                return null;
            }

            System.out.println("value of connection is=="+l); 
            for (int i = 0, s = l.size() ; i < s ; i++) {
                c = (Connection) l.get(i);
                long t;

                t = c.getCreateTime();

                if (t < time) {
                    earliest = c;
                    time = t;
                }
            }

        return earliest;
    }
}

I called CallManger.java like this in my own class

CallManager cm = CallManager.getInstance();

My another class is CallUpdate, it should give me a OutgoingCall states (i.e other side phone is Busy, Power-off, not-reachable etc.) The code is like this:

public class CallUpdate {   

    Call myCall = new MyCall();
    Connection myConn = new MyConnection();
    CallManager cm = CallManager.getInstance();

        public Object getCallFailedString(){

           myConn = myCall.getEarliestConnection();
           System.out.println("myConn is  ******"+myConn);
           System.out.println("myCall is  ******"+myCall);  

           if(myConn == null){
                System.out.println("myConn is null ******");
                return null;
           }                
          else
            {
               Connection.DisconnectCause cause = myConn.getDisconnectCause();                       
               System.out.println("myconn is not null ******"+cause);   


                switch(cause){

                 case BUSY :
                   System.out.println("inside busy");
                 break;

                 case NUMBER_UNREACHABLE :
                    System.out.println("inside un-reachable");
                 break;

                case POWER_OFF :
                   System.out.println("inside power off");
                 break;  
              }
       }
    return myConn;
 }

I called this class in BroadCastReceiver(). But i am getting connection is null. My code is not getting inside else part. So that I added some code in getConnection method of MyCall class like this:

public List<Connection> getConnections() {      


     state = cm.getState();     
     ringingCall = (MyCall) cm.getRingingCalls();
         System.out.println("**inside getConnections="+ringingCall);        
          if ( ringingCall != null && !ringingCall.isIdle()) {
            System.out.println("**call is not null***");
            return ((MyCall) ringingCall).getConnections();
        }      
        else
        {
            System.out.println("**list is  null***");
            return emptyConnections;
        }   
    }

But I am getting java.lang.ClassCastException: on Line: ringingCall = (MyCall) cm.getRingingCalls(); And also on l = getConnection(); How to solve this ?? Thx in advance.

1
  • Can anybody give me a correct answer ??!! Commented Jan 9, 2012 at 12:09

2 Answers 2

1

Change your code as below

List<MyCall> ringingCall = cm.getRingingCalls();

I believe your cm.getRingingCalls(); returning ArrayList of Call / MyCall

0
1

getRingingCalls returns a List<Call> i.e. a list of Call objects. You're trying to cast it to a single MyCall, instead of a List, so you get a ClassCastException.

You need to correct your type to a List:

List<MyCall> ringingCall;
ringingCall = cm.getRingingCalls();

or, use the first element in the list:

MyCall ringingCall;
if(!cm.getRingingCalls().isEmpty()){
    ringingCall = cm.getRingingCalls().get(0);
}
2
  • Thx for ur answer but I am not yet all getting inside if :( Commented Jan 9, 2012 at 10:43
  • I tried it like: if(!cm.getRingingCalls().isEmpty()){ System.out.println("inside if****"); ringingCall = (MyCall) cm.getRingingCalls().get(0); System.out.println("call is not null*"); System.out.println("value of call"+ringingCall); return ((MyCall) ringingCall).getConnections(); } But i am not getting value of call statement Commented Jan 9, 2012 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.