简体   繁体   English

如何修复我的应用程序崩溃而不是拨打电话?

[英]How to fix my app that crash instead of make a call?

I'm trying to make an app that start a call when it receives a specific message.我正在尝试制作一个在收到特定消息时开始通话的应用程序。 Everything is alright except the app crashes when it should start the call.一切都很好,除了应用程序在应该开始呼叫时崩溃。

The problem is at the line where I use startActivity() .问题出在我使用startActivity()的那一行。 I don't know why it crash.我不知道为什么会崩溃。 Can someone help me?有人能帮我吗?

public class SmsBroadcastReceiver extends BroadcastReceiver {

    public static final String SMS_BUNDLE = "pdus";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        //....
            String cod = "message";
            if(smsBody.compareTo(cod) == 0){
                CallClass obj = new CallClass();
                obj.call();
            }
        //....            

private class CallClass extends AppCompatActivity {

       public void call() {
           Intent callIntent = new Intent(Intent.ACTION_CALL);
           callIntent.setData(Uri.parse("tel:+107222222"));
           startActivity(callIntent);
        }
    }

EDIT:编辑:

Also I tried the code below:我也试过下面的代码:

private class CallClass extends AppCompatActivity {
       public void call() {
           Intent callIntent = new Intent(Intent.ACTION_CALL);
           callIntent.setData(Uri.parse("tel:+107222222"));
           try{
               startActivity(callIntent);
           }
           catch(SecurityException e) {
               e.printStackTrace();
           }
       }
    }

But it don't solve my problem.但这并不能解决我的问题。 I checked and I gave all permissions that app needs to start a call.我检查并授予了应用程序开始通话所需的所有权限。 In debugging mode I receive this message:在调试模式下,我收到此消息:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: romania.ovi.smsapp, PID: 9715
    java.lang.RuntimeException: Unable to start receiver romania.ovi.smsapp.SmsBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference   
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:3388)
....
    W/System: A resource failed to call close. 

Make sure you have this in your AndroidManifest.xml确保你的 AndroidManifest.xml 中有这个

 <uses-permission android:name="android.permission.CALL_PHONE" />

And your method should probably look like this:你的方法应该是这样的:

public void call(String message, Context context) {
    if(message.compareTo("SpecificMessage")){

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:+40773733585"));
    startActivity(callIntent);
    }

}

The problem is that you are trying to use an Activity as just another class.问题是您试图将 Activity 用作另一个类。 As a rule of thumb, if your are calling an Activity constructor, you are doing something wrong.根据经验,如果您正在调用 Activity 构造函数,那么您就做错了。 Either the class should not be an activity or you should not be calling the constructor.该类不应该是活动,或者您不应该调用构造函数。

In your use case, you would be better off writing the call code in the BroadcastReceiver itself.在您的用例中,您最好在 BroadcastReceiver 本身中编写调用代码。

@Override
public void onReceive(Context context, Intent intent) {
   ...
   Intent callIntent = new Intent(Intent.ACTION_CALL);
   callIntent.setData(Uri.parse("tel:+107222222"));
   context.startActivity(callIntent);
}

Edit:编辑:

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+107222222"));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 clickListener 只会在我第二次调用它时使我的应用程序崩溃 - Why does my clickListener make my app crash only the second time i call it 如何修复Android应用程序崩溃问题? - How to fix android app crash issues? 为什么按下按钮时我的应用程序崩溃以及如何修复 - Why does my app crash when I press a button and how to fix it 如何解决这个rec​​ylerview问题? 我的代码有什么错误吗? 在应用中唱歌后在运行时崩溃 - how to fix this recylerview issue? is there any error in my code ? on runtime after singing in the app get crash 我在测试我的应用程序时遇到了这个崩溃。 我是一个完整的初学者,无法理解如何解决这个问题,有什么解决方案吗? - I have this crash while testing my app. I am a complete beginner and cant understand how to fix this, any solution? 如何修复ViewPager以防止应用程序出错并崩溃? - How to fix ViewPager to prevent app to getting error and crash? 如何修复getMapAsync(this)崩溃? - How to fix getMapAsync(this) crash? 如何修复 onDataChange 崩溃? - How to fix the onDataChange crash? 为什么我的 ListView 使我的应用程序崩溃而不是列出我的条目? - Why does my ListView crash my app instead of listing my Entries? 如何从Java应用程序拨打语音电话? 如何使该程序正常工作? - How do I make a voice call from my Java app? How do I make this program work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM