bug-android之ActivityNotFoundException

时间:2023-03-08 23:52:18
bug-android之ActivityNotFoundException

应用场景:用于安卓的短信发送功能

异常名称:Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND }

源码如下:

package com.phone.app.phone;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText; import java.util.ArrayList; public class SmsActivity extends AppCompatActivity implements View.OnClickListener{     private  EditText edit = null;
    private  EditText smsEdit = null;     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);         edit = (EditText) findViewById(R.id.edit);//获取文本的对象
        smsEdit = (EditText) findViewById(R.id.sms);//获取文本的对象
//        Button btn = (Button) findViewById(R.id.btn);//获取按钮的对象
//
//        //添加按钮事件监听器
//        btn.setOnClickListener(this);
    }      public void onClick(View view){//定义事件对应处理的方法          String num = edit.getText().toString();//获取文本框中的电话号码
         String smstext = smsEdit.getText().toString();
         Intent intent = new Intent();//创建一个意图          SmsManager sm = SmsManager.getDefault();
         intent.setAction(intent.ACTION_SEND);//设置意图的动作
         intent.setClassName("com.phone.app.phone","com.phone.app.phone.SmsActivity");//设置处理intent的activity
         ArrayList<String> list = sm.divideMessage(smstext);  //因为一条短信有字数限制,因此要将长短信拆分
         for(String text:list){
             sm.sendTextMessage(num, null, text, null, null);
         }          //intent.setData(Uri.parse("tel:"+num));//设置意图的数据
         this.startActivity(intent);//使用意图开启一个界面
     }
}

解决方案:

设置处理intent的activity ,

intent.setClassName("com.phone.app.phone","com.phone.app.phone.SmsActivity");//设置处理intent的activity

即可解决此bug!