ClassCastException错误

时间:2012-01-07 10:54:50

标签: java android classcastexception

我正在做一个Android应用程序,因为我使用了com.android.internal API。它们是Call.java,CallManger.java。对于这些类,我为Call.java创建了子类。您可以在http://hi-android.info/src/com/android/internal/telephony/Call.java.htmlhttp://hi-android.info/src/com/android/internal/telephony/CallManager.java.html找到这两个课程。

Call.java的子类是:

公共类MyCall扩展了Call {

 1.     CallManager cm = CallManager.getInstance(); 
 2.     Phone.State state;
 3.     
 4.      Phone mDefaultPhone;
 5.      private final ArrayList<Connection> emptyConnections = new ArrayList<Connection>();    
 6.      List<Call> ringingCall;
 7.     
 8.     @Override
 9.     public List<Connection> getConnections() {      
 10.        
 11.        
 12.        state = cm.getState();      
 13.        ringingCall = cm.getForegroundCalls();     
 14.        System.out.println("**inside getConnections="+state);   
 15.        System.out.println("**inside getConnections="+ringingCall); 
 16.        if ( ringingCall == null) {
 17.            
 18.            System.out.println("**call is null***");
 19.            return emptyConnections;
 20.         }
 21.         else
 22.         {
 23.            System.out.println("**call is not null***");
 24.            return ((Call) ringingCall).getConnections();   
            }


        }

        @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() {
            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;
            }

            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的实例,就像这样:

 CallManager cm = CallManager.getInstance();

Bcoz这是一个终极修饰符类。我的另一个类是CallUpdate.java。

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

我的myConn值为null。为此,我在MyCall类的getConnection方法中添加了一些代码来获取myConn的非null值。即

 state = cm.getState();     
        ringingCall = cm.getForegroundCalls();     
        System.out.println("**inside getConnections="+state);   
        System.out.println("**inside getConnections="+ringingCall); 
        if ( ringingCall == null) {

            System.out.println("**call is null***");
            return emptyConnections;
        }
        else
        {
            System.out.println("**call is not null***");
            return ((Call) ringingCall).getConnections();   
        }

但它在Line No:24和在线上抛出ClassCastException错误  l = getConnections();。  而且我还需要至少一个拨出电话才能获得myConn的值。如何解决此错误?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

ringingCallList<Call> - 不是一次通话。你可能想要这样的东西:

if (ringingCall != null && !ringingCall.isEmpty()) {
    Call call = ringingCall.get(0);
    // Use call
}

...但如果有多个电话,你还应该考虑你想做什么。

一般情况下,如果你发现自己是因为你认为自己比编译器更清楚。它应该总是至少让你思考 - 如果你想要一个Call,但你有一个List<Call>,自然的方法应该是使用一个元素从列表中。

目前尚不清楚你是否确实首先想要成为Call的子类,说实话。