2014-07-19 60 views
0

即时通讯设法使用按钮调用不同的电话号码的应用程序。我有这个: package com.BigTooth.Apps.Recromax;如何从Android应用程序拨打电话

import android.app.*; 
import android.os.*; 
import android.view.*; 
import android.widget.*; 
import android.content.Intent; 
import android.content.*; 
import android.net.Uri; 
import android.view.View.OnClickListener; 


public class MainActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
    private void phoneCall1() 
    { 
     String phoneCallUri = "tel:4078421430"; 
     Intent phoneCallIntent = new Intent(Intent.ACTION_CALL); 
     phoneCallIntent.setData(Uri.parse(phoneCallUri)); 
     startActivity(phoneCallIntent); 
    } 
    // add button listener 
    button.setOnClickListener(new OnClickListener() { 
    private void phoneCall() 
    { 
     String phoneCallUri = "tel:8889807091"; 
     Intent phoneCallIntent = new Intent(Intent.ACTION_CALL); 
     phoneCallIntent.setData(Uri.parse(phoneCallUri)); 
     startActivity(phoneCallIntent);} 
    } 
} 

在我的MainActivity.java文件中。它告诉我这是不正确的。请帮忙!!!

+0

请发布Logcat输出。另外,您的Manifest中是否有'CALL_PRIVILEGED'权限? – OrhanC1

+1

你的'button.setOnClickListener'在任何函数之外..在错误的范围..把它放在onCreate ..和phoneCall函数也是太乱..太多的copypaste ..试着学习一些基本知识之前开始copypaste :) – Hardy

+0

我确实有我的清单中的权限 – tycemang

回答

1

button.setOnClickListener是在错误的地方。此外,onClickListener有点扭曲。应该是:

import android.app.*; 
import android.os.*; 
import android.view.*; 
import android.widget.*; 
import android.content.Intent; 
import android.content.*; 
import android.net.Uri; 
import android.view.View.OnClickListener; 


public class MainActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button button = (Button) findViewById(R.id.youButtonId); 
     Button button1 = (Button) findViewById(R.id.youButtonId1); 
     // add button listener 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       phoneCall(); 
      } 
     }); 
     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       phoneCall1(); 
      } 
     }); 
    } 
    private void phoneCall() 
    { 
     String phoneCallUri = "tel:8889807091"; 
     Intent phoneCallIntent = new Intent(Intent.ACTION_CALL); 
     phoneCallIntent.setData(Uri.parse(phoneCallUri)); 
     startActivity(phoneCallIntent); 
    } 
    private void phoneCall1() 
    { 
     String phoneCallUri = "tel:4078421430"; 
     Intent phoneCallIntent = new Intent(Intent.ACTION_CALL); 
     phoneCallIntent.setData(Uri.parse(phoneCallUri)); 
     startActivity(phoneCallIntent); 
    } 
} 
+0

它告诉我,button.setOnClickListener(新的OnClickListener(){是一个错误 – tycemang

+0

是的,对不起,我的错,我添加了一个大括号(我不应该删除它) –

+0

我定义了它? – tycemang

相关问题