[英]Getting java.lang.ClassCastException while sending DTMF tones over an active call in android
I am developing an app that sends DTMF tones over an active call in android. 我正在开发一个通过android中的活动呼叫发送DTMF音的应用程序。 I searched a lot to implement this feature in my app and i found this : com.android.internal.telecom.IInCallAdapter interface that extends android.os.IInterface .
我进行了大量搜索以在我的应用中实现此功能,然后发现了这个: com.android.internal.telecom.IInCallAdapter接口,该接口扩展了android.os.IInterface 。 There is a method playDtmfTone(String s,Char c) inside IInCallAdapter that we can use to send DTMF tones over an active call.
IInCallAdapter内部有一个playDtmfTone(String s,Char c) 方法 ,我们可以使用该方法通过活动呼叫发送DTMF音调。 I have an activity HelloPage.java that do so, but getting java.lang.ClassCastException error while initializing an object of IInCallAdapter .
我有一个活动HelloPage.java这样做,但是在初始化IInCallAdapter的对象时遇到java.lang.ClassCastException错误。 Well i know what this error means but don't know how to deal with this.
好吧,我知道这个错误是什么意思,但不知道如何处理。 Here is my code :
这是我的代码:
public class HelloPage extends Activity implements View.OnClickListener{
Button DTMF_b1,DTMF_b2,DTMF_b3,DTMF_b4,DTMF_b5,DTMF_b6,DTMF_b7,DTMF_b8,DTMF_b9,DTMF_b0,DTMF_bS,DTMF_bP;
private TextView DTMFToneDialedView;
private String DTMFToneDialedNumber = "";
private String callId;
private IInCallAdapter mIInCallAdapter;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_hello_page);
mIInCallAdapter = (IInCallAdapter) this; /*I am getting error here in this line*/
DTMF_b1 = (Button) findViewById(R.id.id_playDTMF1);
DTMF_b2 = (Button) findViewById(R.id.id_playDTMF2);
DTMF_b3 = (Button) findViewById(R.id.id_playDTMF3);
DTMF_b4 = (Button) findViewById(R.id.id_playDTMF4);
DTMF_b5 = (Button) findViewById(R.id.id_playDTMF5);
DTMF_b6 = (Button) findViewById(R.id.id_playDTMF6);
DTMF_b7 = (Button) findViewById(R.id.id_playDTMF7);
DTMF_b8 = (Button) findViewById(R.id.id_playDTMF8);
DTMF_b9 = (Button) findViewById(R.id.id_playDTMF9);
DTMF_bS = (Button) findViewById(R.id.id_playDTMFStar);
DTMF_b0 = (Button) findViewById(R.id.id_playDTMF0);
DTMF_bP = (Button) findViewById(R.id.id_playDTMFPound);
callId = MainActivity.telecomCallId; //Getting telecom Call ID from a static variable in a MainActivity
DTMFToneDialedView = (TextView) findViewById(R.id.id_DTMFToneDialedKey);
DTMF_b1.setOnClickListener(this);
DTMF_b2.setOnClickListener(this);
DTMF_b3.setOnClickListener(this);
DTMF_b4.setOnClickListener(this);
DTMF_b5.setOnClickListener(this);
DTMF_b6.setOnClickListener(this);
DTMF_b7.setOnClickListener(this);
DTMF_b8.setOnClickListener(this);
DTMF_b9.setOnClickListener(this);
DTMF_bS.setOnClickListener(this);
DTMF_b0.setOnClickListener(this);
DTMF_bP.setOnClickListener(this);
}
//This method updated the screen what we dial
public void dialedNumberUpdater(String key){
DTMFToneDialedNumber = DTMFToneDialedNumber + key;
DTMFToneDialedView.setText(DTMFToneDialedNumber);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.id_playDTMF1:
try {
mIInCallAdapter.playDtmfTone(callId, '1');
dialedNumberUpdater("1");
}catch (Exception e){
dialedNumberUpdater("w"); //updating 'w' into the screen so that user can see it is not working.
}
break;
case R.id.id_playDTMF2:
try {
mIInCallAdapter.playDtmfTone(callId, '2');
dialedNumberUpdater("2");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMF3:
try {
mIInCallAdapter.playDtmfTone(callId, '3');
dialedNumberUpdater("3");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMF4:
try {
mIInCallAdapter.playDtmfTone(callId, '4');
dialedNumberUpdater("4");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMF5:
try {
mIInCallAdapter.playDtmfTone(callId, '5');
dialedNumberUpdater("5");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMF6:
try {
mIInCallAdapter.playDtmfTone(callId, '6');
dialedNumberUpdater("6");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMF7:
try {
mIInCallAdapter.playDtmfTone(callId, '7');
dialedNumberUpdater("7");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMF8:
try {
mIInCallAdapter.playDtmfTone(callId, '8');
dialedNumberUpdater("8");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMF9:
try {
mIInCallAdapter.playDtmfTone(callId, '9');
dialedNumberUpdater("9");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMFStar:
try {
mIInCallAdapter.playDtmfTone(callId, '*');
dialedNumberUpdater("*");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMF0:
try {
mIInCallAdapter.playDtmfTone(callId, '0');
dialedNumberUpdater("0");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
case R.id.id_playDTMFPound:
try {
mIInCallAdapter.playDtmfTone(callId, '#');
dialedNumberUpdater("#");
}catch (Exception e){
dialedNumberUpdater("w");
}
break;
}
}
}
Here is the Logcat : 这是Logcat:
01-29 01:43:07.649 565-1247/? I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=algor7.code.backgroundcallingapp/.HelloPage} from uid 10099 on display 0
01-29 01:43:07.794 13439-13439/algor7.code.backgroundcallingapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: algor7.code.backgroundcallingapp, PID: 13439
java.lang.RuntimeException: Unable to start activity ComponentInfo{algor7.code.backgroundcallingapp/algor7.code.backgroundcallingapp.HelloPage}: java.lang.ClassCastException: algor7.code.backgroundcallingapp.HelloPage cannot be cast to android.os.IInterface
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: algor7.code.backgroundcallingapp.HelloPage cannot be cast to android.os.IInterface
at algor7.code.backgroundcallingapp.HelloPage.onCreate(HelloPage.java:39)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-29 01:43:07.813 565-876/? W/ActivityManager﹕ Force finishing activity algor7.code.backgroundcallingapp/.HelloPage
01-29 01:43:08.345 565-578/? W/ActivityManager﹕ Activity pause timeout for ActivityRecord{454bc33 u0 algor7.code.backgroundcallingapp/.HelloPage t191 f}
Please help me to solve this issue. 请帮我解决这个问题。 Thank you in advance.
先感谢您。
Because your this is a HelloPage object. 因为您这是一个HelloPage对象。 It is not an IInCallAdapter, does not inherit from it, and does not extend it.
它不是IInCallAdapter,不会继承它,也不会扩展它。 It can't be cast as one.
它不能转换为一个。 Why would you think it could be?
为什么会这样呢?
Create a class that implements IInCallAdapter and then implement all its methods 创建一个实现IInCallAdapter的类,然后实现其所有方法
public class ClassName implements IInCallAdapter {
...
}
Then you can create an instance of this class in your Activity class 然后,您可以在Activity类中创建此类的实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.