2013-02-26 75 views
-2

我正在製作一個SMS應用程序,用戶在該聯繫人和消息中鍵入內容,然後單擊按鈕butenc。該按鈕必須將可變電話號碼和消息發送到另一個Java類,方法接收數據並對其進行處理。 Ecc java文件是一個獨立的java類,它鏈接到各種其他的java類,以便對消息進行操作。我有時得到一個stackoverflow錯誤,當我在代碼中操作一些東西時,應用程序運行,當我單擊enc按鈕什麼都沒有發生,但是當我單擊發送按鈕時發送消息。我相信我在將變量傳遞給Ecc類時遇到了問題。我在操作系統上遇到了各種各樣的問題,但他們都沒有解決這個問題。我所做的改變給了我上面提到的任何一個問題。我如何克服這個問題?無法從活動類中調用java類的方法

SMSTest.java(主機器人活性)

package com.example.smsTest; 

import android.annotation.SuppressLint; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Build; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 


@TargetApi(Build.VERSION_CODES.DONUT) 
@SuppressLint("NewApi") 
public class SMSTest extends Activity 
{ 
    public final static String SMS_Message = "com.example.SMSTest.MESSAGE"; 
public final static String SMS_Phone = "com.example.SMSTest.MESSAGE"; 
Button btnSendSMS; 
Button btnEnc; 
EditText txtPhoneNo; 
EditText txtMessage; 

軟腐病ECC;

/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);   
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS); 
    btnEnc = (Button) findViewById(R.id.btnEnc); 
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo); 
    txtMessage = (EditText) findViewById(R.id.txtMessage); 

    ecc=new Ecc(this); 

    /* 
    Intent sendIntent = new Intent(Intent.ACTION_VIEW); 
    sendIntent.putExtra("sms_body", "Content of the SMS goes here..."); 
    sendIntent.setType("vnd.android-dir/mms-sms"); 
    startActivity(sendIntent); 
    */ 

    btnEnc.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     {    
      String phoneNo = txtPhoneNo.getText().toString(); 
      String message = txtMessage.getText().toString(); 
      if (phoneNo.length()>0 && message.length()>0){ 
       System.out.println("details verified"); 
       ecc.recv(phoneNo, message);  
       Toast.makeText(getBaseContext(), 
        "Ecc started", 
        Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    btnSendSMS.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     {    
      String phoneNo = txtPhoneNo.getText().toString(); 
      String message = txtMessage.getText().toString();    
      if (phoneNo.length()>0 && message.length()>0)     
       sendSMS(phoneNo, message); 
      else 
       Toast.makeText(getBaseContext(), 
        "Please enter both phone number and message.", 
        Toast.LENGTH_SHORT).show(); 
     } 
    });   
} 

//---sends a SMS message to another device--- 
@TargetApi(Build.VERSION_CODES.DONUT) 
@SuppressLint("NewApi")  
public void sendSMS(String phoneNumber, String message) 
{  
    /* 
    PendingIntent pi = PendingIntent.getActivity(this, 0, 
      new Intent(this, test.class), 0);     
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phoneNumber, null, message, pi, null);   
    */ 

    String SENT = "SMS_SENT"; 
    String DELIVERED = "SMS_DELIVERED"; 

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, 
     new Intent(SENT), 0); 

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, 
     new Intent(DELIVERED), 0); 

    //---when the SMS has been sent--- 
    registerReceiver(new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) 
      { 
       case Activity.RESULT_OK: 
        Toast.makeText(getBaseContext(), "SMS sent", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
        Toast.makeText(getBaseContext(), "Generic  failure", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_NO_SERVICE: 
        Toast.makeText(getBaseContext(), "No service", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_NULL_PDU: 
        Toast.makeText(getBaseContext(), "Null PDU", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_RADIO_OFF: 
        Toast.makeText(getBaseContext(), "Radio off", 
          Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 
    }, new IntentFilter(SENT)); 

    //---when the SMS has been delivered--- 
    registerReceiver(new BroadcastReceiver(){ 
     @TargetApi(Build.VERSION_CODES.DONUT) 
     @SuppressLint("NewApi") 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) 
      { 
       case Activity.RESULT_OK: 
        Toast.makeText(getBaseContext(), "SMS delivered", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case Activity.RESULT_CANCELED: 
        Toast.makeText(getBaseContext(), "SMS not delivered", 
          Toast.LENGTH_SHORT).show(); 
          break;     
      } 
     } 
    },new IntentFilter(DELIVERED));   

    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);    
    } 
} 

這裏我其他類(Ecc.java)

package com.example.smsTest; 
import java.util.*; 
public class Ecc{ 

//code 
private SMSTest smsTest; 


// Constructor 
public Ecc(SMSTest smsTest) { 
    this.smsTest = smsTest; 
} 


public void recv(String phn, String smsg){ 

/*performs manipulation on phn and msg taken 
from the main activity using various other 
java files linked with it and again sends it 
back to themaina ctivities sendSMS method*/ 
smsTest.sendSMS(pnh,smsg) 
} 

//code 
} 

我的main.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter the phone number of recipient" 
    />  
<EditText 
    android:id="@+id/txtPhoneNo" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"   
    /> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"   
    android:text="Message" 
    />  
<EditText 
    android:id="@+id/txtMessage" 
    android:layout_width="fill_parent" 
    android:layout_height="150px" 
    android:gravity="top"   
    /> 

<Button 
    android:id="@+id/btnEnc" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Ecc" /> 

<Button 
    android:id="@+id/btnSendSMS" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Send SMS" 
    /> 

</LinearLayout> 

當我點擊發送按鈕被髮送的消息,但是當我點擊ECC ecc類負載啓動...

這裏是我的logcat

02-26 13:04:36.337: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:36.447: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:36.447: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:36.797: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:36.797: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.077: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.117: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.247: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.247: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.318: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.391: I/Choreographer(1265): Skipped 74 frames! The application may be doing too much work on its main thread. 
02-26 13:04:37.416: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.416: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.427: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.427: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.437: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
02-26 13:04:37.447: W/Trace(1265): Unexpected value from nativeGetEnabledTags: 0 
+1

什麼是你的問題,你在哪裏得到錯誤,你可以發佈你的logcat錯誤 – 2013-02-26 09:38:55

回答

0

當你實例化一個對象Ecc,它創建了一個SMSTest對象,釋創建一個Ecc對象,它創建了一個SMSTest對象,循環往復。這會產生堆棧溢出。

這不可能是正確的。

您需要在您的Ecc類是這樣的:

// Reference to the SMSTest instance 
private SMSTest smsTest; 

// Constructor 
public Ecc(SMSTest smsTest) { 
    this.smsTest = smsTest; 
} 

SMSTest,改變

Ecc ecc=new Ecc(); 

Ecc ecc; // This variable gets initialized in onCreate() 

然後,在SMSTest.onCreate()做:

ecc=new Ecc(this); 
+0

謝謝你這麼多,但我現在有按鈕的問題。我沒有遇到stackoverflow錯誤,但是當我點擊按鈕時,它並沒有做任何事情 – Rad 2013-02-26 12:52:08

+0

添加一些日誌記錄或設置斷點,看看你的'onClick()'方法是否被調用。 – 2013-02-26 13:01:28

+0

我檢查了該方法正在被調用,但ecc類不加載。我用更新後的代碼編輯了問題。 – Rad 2013-02-26 13:12:59

0

歡迎來到StackoverFlow

你的方法是錯誤的。

活動永遠不會以這種方式創建。 SMSTest smstest=new SMSTest();

當您創建Ecc對象並在該活動中使用該引用來調用任何方法時,它最好通過對此活動的引用。

0

在java類中創建靜態方法,並在具有類名稱的活動中調用該方法,反之亦然。意味着如果你想調用java類中的activity方法,那麼使該方法在活動中是靜態的,並通過actvity名稱調用該方法。

+0

在這種情況下,很多庫方法都不起作用,因爲它們本身並不是靜態的。 – Rad 2013-02-26 12:52:52