您好刚刚开始在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>
MainActvity.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.
答案 0 :(得分:1)
尝试
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1121121212"));
startActivity(callIntent);
答案 1 :(得分: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);
}
});
并在下面的Manifest中添加权限
<uses-permission android:name="android.permission.CALL_PHONE" />
有关详细信息see here
答案 2 :(得分:0)
Logcat消息和布局不是同一次运行。
Logcat错误清楚地说:
无法在活动类com.example.androidproject2.MainActivity中找到方法showDialer(View),用于视图类android.widget.Button上的onClick处理程序,其id为“btnShow”
这意味着您的真实布局不是您在此处发布的内容。我很确定它包含这样的内容:
<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()。