什么是 java.lang.ClassCastException: java.util.ArrayList
我的代码抛出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改您的代码如下
List; ringingCall = cm.getRingingCalls();
我相信您的
cm.getRingingCalls();
返回Call
/的
ArrayList
我的呼叫Change your code as below
List<MyCall> ringingCall = cm.getRingingCalls();
I believe your
cm.getRingingCalls();
returningArrayList
ofCall
/MyCall
getRingingCalls
返回一个List
,即Call
对象的列表。您尝试将其转换为单个MyCall
,而不是List
,因此您会收到ClassCastException
。您需要将类型更正为
List
:或者使用列表中的第一个元素:
getRingingCalls
returns aList<Call>
i.e. a list ofCall
objects. You're trying to cast it to a singleMyCall
, instead of aList
, so you get aClassCastException
.You need to correct your type to a
List
:or, use the first element in the list: