-2

我正在制作一个短信应用程序,用户在其中输入联系人和消息,然后单击按钮 butenc。该按钮必须将可变电话号码和消息发送到另一个 java 类,其中一个方法接收数据并对其进行操作。Ecc java 文件是一个独立的 java 类,它链接到各种其他 java 类以对消息进行操作。我有时会遇到 stackoverflow 错误,当我在代码中操作一些东西时,应用程序会运行,当我单击 enc 按钮时没有任何反应,但是当我单击发送按钮时,消息被发送。我确定我在将变量传递给 Ecc 类时遇到了问题。我在操作系统上遇到了各种问题,但没有一个能解决问题。我所做的更改给出了我上面提到的任何一个问题。我该如何克服这个问题?

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;

电子抄送电子抄送;

/** 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 类加载开始...

这是我的日志猫

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
4

3 回答 3

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);
于 2013-02-26T10:27:03.287 回答
0

欢迎来到StackoverFlow

你的方法是错误的。

活动永远不会以这种方式创建。SMSTest smstest=new SMSTest();

最好在创建对象时传递对此活动的引用Ecc,然后使用该引用来调用活动中的任何方法。

于 2013-02-26T10:29:51.157 回答
0

在java类中创建静态方法并在活动中使用类名调用它。反之亦然。意思是如果你想在java类中调用活动方法然后在活动中将该方法设为静态并通过活动名称调用该方法。

于 2013-02-26T11:14:17.020 回答