通过android中的活动调用发送DTMF音时获取java.lang.ClassCastException [英] Getting java.lang.ClassCastException while sending DTMF tones over an active call in android

查看:78
本文介绍了通过android中的活动调用发送DTMF音时获取java.lang.ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个通过android中的活动呼叫发送DTMF音的应用程序.我进行了很多搜索以在我的应用中实现此功能,并发现了以下内容:扩展了 android.os.IInterface com.android.internal.telecom.IInCallAdapter 接口.在 IInCallAdapter 中有一种方法 playDtmfTone(String s,Char c),我们可以使用该方法通过当前通话发送DTMF音调.我有一个活动 HelloPage.java 可以这样做,但是在初始化 IInCallAdapter 的对象时出现 java.lang.ClassCastException 错误.好吧,我知道这个错误是什么意思,但不知道如何处理.这是我的代码:

I am developing an app that sends DTMF tones over an active call in android. 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. There is a method playDtmfTone(String s,Char c) inside IInCallAdapter that we can use to send DTMF tones over an active call. I have an activity HelloPage.java that do so, but getting java.lang.ClassCastException error while initializing an object of IInCallAdapter. 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;

    }

}

}

这是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.

推荐答案

创建一个实现IInCallAdapter的类,然后实现其所有方法

Create a class that implements IInCallAdapter and then implement all its methods

public class ClassName implements IInCallAdapter {
    ... 
} 

然后您可以在Activity类中创建此类的实例

Then you can create an instance of this class in your Activity class

这篇关于通过android中的活动调用发送DTMF音时获取java.lang.ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆