2014-07-19 80 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

相關問題