获得空值

时间:2012-01-05 16:12:51

标签: java android

我正在使用com.android.internal.telephony API。在那里我调用了两个抽象类,它们是Call.java和Connection.java。您可以在http://hi-android.info/src/com/android/internal/telephony/Call.java.htmlhttp://hi-android.info/src/com/android/internal/telephony/Connection.java.html找到这些创建的子类,如

Call myCall = new MyCall();
Connection myConn = new MyConnection();

我需要使用连接类中的getDisconnectCause方法,这是一个抽象方法,我这样使用:

myConn = myCall.getEarliestConnection();                            
      if(myConn == null){
         System.out.println("myConn is null ******");   
        }else
           {
            Connection.DisconnectCause cause = myConn.getDisconnectCause();                      
                System.out.println("value of cause ******"+cause);  
           }

Call.java的子类是:

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

                            }

                   @Override
                   public boolean isMultiparty() {
                    return false;
                   }        
                   public Connection
                       getEarliestConnection() {
                            List l;
                                long time = Long.MAX_VALUE;
                                Connection c;
                                Connection earliest = null;

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

                        }

AND the Connection.java subclass is:


    class MyConnection extends Connection{

            @Override
            public void cancelPostDial() {
                // TODO Auto-generated method stub

            }

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

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

            @Override
            public long getConnectTime() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public long getCreateTime() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public DisconnectCause getDisconnectCause() {
                // TODO Auto-generated method stub


                return null;
            }

            @Override
            public long getDisconnectTime() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public long getDurationMillis() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public long getHoldDurationMillis() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public int getNumberPresentation() {
                // TODO Auto-generated method stub
                return 0;
            }

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

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

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

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

            }

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

            @Override
            public void proceedAfterWaitChar() {
                // TODO Auto-generated method stub

            }

            @Override
            public void proceedAfterWildChar(String arg0) {
                // TODO Auto-generated method stub

            }

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

            } 


        }  

EDIT 2 : I have edited line number 1 to 17. Plz check this. I am getting java.lang.ClassCastException: java.util.Collections error on Line No: 16 and Line No:68.任何人都可以帮我解决这个问题。此外,即使呼叫不为空,我也只能获得一种呼叫状态,即IDLE。我正在进入其他部分。请帮助我。

3 个答案:

答案 0 :(得分:1)

 @Override
 public List<Connection> getConnections() {
    return null;
 }

MyCall类上的此方法返回null,如果MyCall.getEarliestConnection();返回getConnections(),则null的代码返回null。

答案 1 :(得分:1)

getConnections()方法的实现使其返回null。

getEarliestConnection()内的代码可以看出,如果getConnections()返回null,那么getEarliestConnection()也将返回null。

以下是相关代码:

@Override
public List<Connection> getConnections() {
   return null;
}

...

l = getConnections();
if (l == null) {
return null;
}

答案 2 :(得分:0)

我不知道这段代码应该做什么,但很清楚为什么那时候为空。

在“MyCall.getEarliestConnection()”中,它调用它自己的“getConnections()”方法,该方法只返回n​​ull。