我的代碼拋出java.lang.ClassCastException異常錯誤,爲什麼這個錯誤會得到?我正在使用com.android.internal.telephony API。即你可以找到類的,我在這裏使用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>
我創建Call.java的Subclaas這樣的:什麼是java.lang.ClassCastException:java.util.ArrayList
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;
}
我在BroadCastReceiver()中調用了這個類。但我得到的連接是空的。我的代碼沒有進入其他部分。所以,我在的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();
如何解決這個問題? Thx提前。
任何人都可以給我一個正確的答案?? !! – shiv1229 2012-01-09 12:09:34