Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 调用函数";startActivity“;在其他类和文件中';行不通_Android_Android Activity - Fatal编程技术网

Android 调用函数";startActivity“;在其他类和文件中';行不通

Android 调用函数";startActivity“;在其他类和文件中';行不通,android,android-activity,Android,Android Activity,我想通过两种方式调用函数startActivity: 首先(它起作用): public class HelloWorld extends Activity { public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == 1){ startActivity(new Intent(Intent.ACTION_DIAL,U

我想通过两种方式调用函数startActivity:

首先(它起作用):

        public class HelloWorld extends Activity
{
        public boolean onOptionsItemSelected(MenuItem item) {
            if(item.getItemId() == 1){
                startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse("tel:660718109")));
            }
            else {
                return super.onOptionsItemSelected(item);
            }
            return true;
}
秒: 在HelloActivity.java中

    public class HelloWorld extends Activity {
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == 1){
            IntentsUtils.tryOneOfThese(this);
        }
        else {
            return super.onOptionsItemSelected(item);
        }
        return true;
}
在IntentsUtils.java中

public class IntentsUtils
{
   public static void tryOneOfThese(Activity activity)
   {
       IntentsUtils.call(activity);
   }
   public static void call(Activity activity)
   {
      Intent intent = new Intent(Intent.ACTION_CALL);
      intent.setData(Uri.parse("tel:5555555555"));
      Log.v("MyLogs", "It's works!");
      activity.startActivity(intent);
   }
}
第二种方法不起作用——当我点击菜单中的“位置”时,我在应用中出现了一个错误。我知道IntentsUtils中的函数“call”起作用,因为analise在“LogCat”中登录

这是我在LogCat上的日志: android.permission.CALL\u手机

android.permission.CALL_PHONE>10-27 16:10:56.702:

警告/活动管理器(52):权限 否认:开始意图{ act=android.intent.action.CALL dat=tel:5555 flg=0x10000000 cmp=com.android.phone/.OutgoingCallBroadcaster }来自ProcessRecord{43dbf4b8 363:com.androidbook/10025}(pid=363, uid=10025)需要 android.permission.CALL\u手机

10-27 16:10:56.722: WARN/dalvikvm(363):threadid=3:线程 以未捕获的异常退出 (组=0x4001b188)

10-27 16:10:56.722: 错误/AndroidRuntime(363):未捕获 处理程序:由于以下原因而退出主线程 未捕获异常

10-27 16:10:56.752: 错误/AndroidRuntime(363): java.lang.SecurityException: 权限拒绝:启动意图{ act=android.intent.action.CALL dat=tel:5555 flg=0x10000000 cmp=com.android.phone/.OutgoingCallBroadcaster }来自ProcessRecord{43dbf4b8 363:com.androidbook/10025}(pid=363, uid=10025)需要 android.permission.CALL\u PHONE android.permission.CALL\u PHONE

提前感谢,,
Michael

尝试设置中给出的标记“活动”和“新建”任务

)

您可能需要这样设置:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

我还没试过这个。但是值得一试

问题在于你的行动

仅限于特定的应用程序(我认为仅限于系统应用程序)。您收到logcat消息的原因是,您在尝试启动活动之前记录了它。将发生权限错误,因为系统不允许您的应用程序直接拨打电话号码。您可以拨打,但用户必须按call

这是一个安全问题,如果他们允许任何应用程序开始拨打电话号码

从Android SDK文档:

注意:对哪些应用程序可以发起调用有限制;大多数应用程序都应该使用

编辑
查看上次编辑后,使用logcat。我可以百分之百地说,你的问题在于使用行动电话


长话短说,您没有使用ACTION_CALL的权限,您必须使用ACTION_DIAL。

您能在LogCat中发布您得到的确切错误吗?我添加了这个,但它没有帮助:(当我替换函数ACTION_CALL to ACTION_DIAL时,应用程序工作正常。谢谢;)