2013-02-26 3 views
-2

에서 Java 클래스의 메소드를 호출 할 수 없습니다. 사용자가 연락처와 메시지를 입력 한 다음 버튼 butenc을 클릭하는 sms 애플리케이션을 만들고 있습니다. 버튼은 메소드가 데이터를 수신하고 조작하는 다른 자바 클래스에 가변 전화 번호와 메시지를 보내야합니다. Ecc java 파일은 메시지 조작을위한 다양한 다른 Java 클래스에 링크되는 독립형 Java 클래스입니다. 몇 번 stackoverflow 오류가 발생하고 코드에서 몇 가지를 조작하면 응용 프로그램이 실행되고 enc 버튼을 클릭해도 아무 일도 일어나지 않지만 보내기 버튼을 클릭하면 메시지가 전송됩니다. Ecc 클래스에 변수를 전달할 때 문제가 있다고 확신합니다. OS에 관한 여러 가지 질문을했지만 그 중 누구도 문제를 해결하지 못했습니다. 내가 변경 한 것은 위에 언급 한 문제 중 하나를 제공합니다. 이 문제를 어떻게 극복 할 수 있습니까?활동 클래스

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 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

당신이 Ecc 개체를 인스턴스화 할 때 17,451,515,
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 오류를 게시 할 수 있습니까? –

답변

0

, 그것은 틱은 SMSTest 객체, 무한히 생성하는 Ecc 객체를 생성, SMSTest 개체를 만듭니다. 스택 오버 플로우가 발생합니다.

이것은 옳지 않을 수 있습니다.

당신은 당신의 Ecc 클래스에서이 같은 필요 SMSTest.onCreate()에, 그리고

Ecc ecc=new Ecc(); 

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

을 변경, SMSTest에서

// Reference to the SMSTest instance 
private SMSTest smsTest; 

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

은 수행

ecc=new Ecc(this); 
+0

고맙습니다. 버튼에 문제가 있습니다. 나는 stackoverflow 오류가 발생하지 않지만 버튼을 클릭하면 아무 것도하지 않는다. – Rad

+0

로깅을 추가하거나 중단 점을 설정하고 onClick() 메서드가 호출되는지 확인한다. –

+0

메서드가 호출되고 있지만 ecc 클래스가로드되지 않았는지 확인했습니다. 나는 업데이트 된 코드로 질문을 편집했다. – Rad

0

Welcome to StackoverFlow!

접근 방법이 잘못되었습니다.

활동이 이렇게 생성되지 않습니다. SMSTest smstest=new SMSTest();

Ecc 개체를 만들 때이 활동에 대한 참조를 전달한 다음 해당 참조를 사용하여 활동의 모든 메서드를 호출하는 것이 좋습니다.

0

은 자바 클래스의 정적 메소드를 만들고 그 반대로 할 수 선생의 클래스와 활동에 그를 호출합니다.Java 클래스에서 activity 메소드를 호출하고 해당 메소드를 activity에서 정적으로 만들고 해당 메소드를 actvity name으로 호출하려는 경우를 의미합니다.

+0

이 경우 많은 라이브러리 메소드가 정적이 아니기 때문에 작동하지 않습니다. – Rad

관련 문제