0

您好,现在开始在 Android 中构建小型应用程序。我尝试通过调用 Call Intent 来启动按钮单击呼叫。我已经在 Manifest 文件中设置了必要的权限。

但我仍然收到错误消息

com.android.phone 停止工作

在此处输入图像描述

<Linear Layout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnShow"
    android:text="@string/btnShow"
    android:fontFamily="sans-serif" />

      </LinearLayout>

MainActivity.java

package com.example.androidproject2;

import android.app.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnShowDialer=(Button) findViewById(R.id.btnShow);
        btnShowDialer.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
            try
            { Intent callIntent=new Intent(android.content.Intent.ACTION_CALL,Uri.parse("tel:1121121212"));
                startActivity(callIntent);

            }
            catch(Exception e)
            {
                Log.e("Trail","Call Falied", e);
            }

            }
        });
    }

}

日志输出

08-08 09:29:28.378: D/gralloc_goldfish(862): Emulator without GPU emulation detected.
08-08 09:29:37.548: D/AndroidRuntime(862): Shutting down VM
08-08 09:29:37.608: W/dalvikvm(862): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-08 09:29:37.688: E/AndroidRuntime(862): FATAL EXCEPTION: main
08-08 09:29:37.688: E/AndroidRuntime(862): java.lang.IllegalStateException: Could not find a method showDialer(View) in the activity class com.example.androidproject2.MainActivity for onClick handler on view class android.widget.Button with id 'btnShow'
08-08 09:29:37.688: E/AndroidRuntime(862):  at android.view.View$1.onClick(View.java:3620)
08-08 09:29:37.688: E/AndroidRuntime(862):  at android.view.View.performClick(View.java:4240)
08-08 09:29:37.688: E/AndroidRuntime(862):  at android.view.View$PerformClick.run(View.java:17721)
08-08 09:29:37.688: E/AndroidRuntime(862):  at android.os.Handler.handleCallback(Handler.java:730)
08-08 09:29:37.688: E/AndroidRuntime(862):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-08 09:29:37.688: E/AndroidRuntime(862):  at android.os.Looper.loop(Looper.java:137)
08-08 09:29:37.688: E/AndroidRuntime(862):  at android.app.ActivityThread.main(ActivityThread.java:5103)
08-08 09:29:37.688: E/AndroidRuntime(862):  at java.lang.reflect.Method.invokeNative(Native Method)
08-08 09:29:37.688: E/AndroidRuntime(862):  at java.lang.reflect.Method.invoke(Method.java:525)
08-08 09:29:37.688: E/AndroidRuntime(862):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-08 09:29:37.688: E/AndroidRuntime(862):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-08 09:29:37.688: E/AndroidRuntime(862):  at dalvik.system.NativeStart.main(Native Method)
08-08 09:29:37.688: E/AndroidRuntime(862): Caused by: java.lang.NoSuchMethodException: showDialer [class android.view.View]
08-08 09:29:37.688: E/AndroidRuntime(862):  at java.lang.Class.getConstructorOrMethod(Class.java:423)
08-08 09:29:37.688: E/AndroidRuntime(862):  at java.lang.Class.getMethod(Class.java:787)
08-08 09:29:37.688: E/AndroidRuntime(862):  at android.view.View$1.onClick(View.java:3613)
08-08 09:29:37.688: E/AndroidRuntime(862):  ... 11 more
08-08 09:39:06.209: D/gralloc_goldfish(931): Emulator without GPU emulation detected.
08-08 09:39:23.398: I/Choreographer(931): Skipped 82 frames!  The application may be doing too much work on its main thread.
08-08 09:39:26.398: I/Choreographer(931): Skipped 110 frames!  The application may be doing too much work on its main thread.
4

3 回答 3

1

试试看

Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:1121121212"));
                startActivity(callIntent);
于 2013-08-08T10:13:15.533 回答
0

试试看

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

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

            }

        });

并确保在清单中添加以下权限

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

有关更多详细信息,请参见此处

于 2013-08-08T10:39:56.417 回答
0

Logcat 消息和您的布局不是来自同一运行。

Logcat 错误清楚地表明:

在 id 为“btnShow”的视图类 android.widget.Button 上的 onClick 处理程序的活动类 com.example.androidproject2.MainActivity 中找不到方法 showDialer(View)

这意味着您的真实布局不是您在此处发布的内容。我很确定它包含这样的内容:

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnShow"
    android:text="@string/btnShow"
    android:fontFamily="sans-serif"
    android:onClick="showDialer" />

注意android:onClick="showDialer",这就是 Logcat 告诉你的。

从布局中删除该行,或者创建方法 showDialer 并删除 setOnClickListener()。

于 2014-08-20T03:20:06.300 回答