2014-10-03 240 views
0

我是一名新的android开发人员,我想创建一个android应用程序,当我单击按钮时拨打电话。拨打电话回复Onclick

我的布局XML是:

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Selectionner l&apos;IMSI" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="16dp" 
    android:onClick="SendMessage" 
    android:text="call" /> 

</RelativeLayout> 

的SendMessage函数了Methode在MainActivity.java定义为:

public void sendMessage(View view) { 

// do something to answer the button click 

    Uri number = Uri.parse("tel:333"); 
    Intent callIntent = new Intent(Intent.ACTION_CALL, number); 
    startActivity(callIntent); 

} 

而且我加入许可清单文件:

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

当我点击按钮的消息“不幸的是应用程序已停止“出现!我怎样才能解决这个问题?

回答

3

您在XML android:onClick="SendMessage"声明的,因此您的活动期待

public void SendMessage(View view)

public void sendMessage(View view)

+0

It Work !!!谢谢,我没有注意到这个重要的细节 – user3873819 2014-10-03 09:22:29

+0

不用客气 – Blackbelt 2014-10-03 09:23:02

0
  Intent callIntent = new Intent(Intent.ACTION_CALL); 
      String phoneNumber = editText.getText().toString(); 
      callIntent.setData(Uri.parse("tel:" + phoneNumber)); 
      startActivity(callIntent); 

许可

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