什么是 java.lang.ClassCastException: java.util.ArrayList

发布于 2024-12-25 10:14:59 字数 4912 浏览 0 评论 0原文

我的代码抛出 java.lang.ClassCastException 异常错误,为什么会出现此错误?我正在使用 com.android.internal.telephony API。即你可以找到我在这里使用的类 Call.java, CallManger.java 我创建了一个 Call.java 的子类,如下所示:

    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;
    }
}

我调用了 CallManger .java 像这样在我自己的类中

CallManager cm = CallManager.getInstance();

我的另一个类是 CallUpdate,它应该给我一个 OutgoingCall 状态(即另一方电话忙、关机、无法接通等)代码如下:

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;
 }

我在广播接收器()。但我得到的连接为空。我的代码没有进入其他部分。因此,我在 MyCall 类的 getConnection 方法中添加了一些代码,如下所示:

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;
        }   
    }

但我收到 java.lang.ClassCastException: 在线: ringingCall = (MyCall) cm.getRingingCalls(); 还有 l = getConnection(); 怎么解决这个问题?? 提前谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2025-01-01 10:14:59

更改您的代码如下

List; ringingCall = cm.getRingingCalls();

我相信您的 cm.getRingingCalls(); 返回 Call / ArrayList我的呼叫

Change your code as below

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

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

歌入人心 2025-01-01 10:14:59

getRingingCalls 返回一个 List,即 Call 对象的列表。您尝试将其转换为单个 MyCall,而不是 List,因此您会收到 ClassCastException

您需要将类型更正为 List

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

或者使用列表中的第一个元素:

MyCall ringingCall;
if(!cm.getRingingCalls().isEmpty()){
    ringingCall = cm.getRingingCalls().get(0);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文