在android中的活动调用上发送DTMF音调时获取java.lang.ClassCastException

时间:2016-01-28 22:17:57

标签: android dtmf

我正在开发一个应用程序,通过android中的活动调用发送DTMF音调。我在我的应用程序中搜索了很多以实现此功能,我发现了这个: com.android.internal.telecom.IInCallAdapter 扩展 android.os.IInterface 的界面。在 IInCallAdapter 中有一个方法 playDtmfTone(String s,Char c),我们可以用它来通过活动呼叫发送DTMF音。我有一个活动 HelloPage.java 这样做,但在初始化 IInCallAdapter 的对象时出现 java.lang.ClassCastException 错误。我知道这个错误意味着什么,但不知道如何处理这个问题。这是我的代码:

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}

请帮我解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:0)

因为你的这是一个HelloPage对象。它不是IInCallAdapter,不从它继承,也不扩展它。它不能一体化。为什么你认为它可能是?

答案 1 :(得分:0)

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

public class ClassName implements IInCallAdapter {
    ... 
} 

然后,您可以在Activity类

中创建此类的实例