无法从活动类调用java类的方法
我正在制作一个sms应用程序,用户在其中键入联系人和消息,然后单击按钮butenc。该按钮必须将可变电话号码和消息发送到另一个java类,其中一个方法接收数据并对其进行操作。Ecc java文件是一个独立的java类,它链接到各种其他java类,用于对消息进行操作。我有时会遇到stackoverflow错误,当我操作代码中的一些内容时,应用程序会运行,当我单击enc按钮时,什么也不会发生,但当我单击send按钮时,消息会被发送。我确信我在将变量传递给Ecc类时遇到了问题。我在这里讨论了关于操作系统的各种问题,但没有一个能解决这个问题。我所做的改变带来了我上面提到的任何一个问题。我如何克服这个问题 java(主要android活动) Ecc-Ecc无法从活动类调用java类的方法,java,android,Java,Android,我正在制作一个sms应用程序,用户在其中键入联系人和消息,然后单击按钮butenc。该按钮必须将可变电话号码和消息发送到另一个java类,其中一个方法接收数据并对其进行操作。Ecc java文件是一个独立的java类,它链接到各种其他java类,用于对消息进行操作。我有时会遇到stackoverflow错误,当我操作代码中的一些内容时,应用程序会运行,当我单击enc按钮时,什么也不会发生,但当我单击send按钮时,消息会被发送。我确信我在将变量传递给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)
我的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
对象时,它会创建一个SMSTest
对象,thich会创建一个Ecc
对象,该对象会无限地创建一个SMSTest
对象。这会产生堆栈溢出
这不可能是对的
在您的Ecc
课程中,您需要这样的内容:
// Reference to the SMSTest instance
private SMSTest smsTest;
// Constructor
public Ecc(SMSTest smsTest) {
this.smsTest = smsTest;
}
在SMSTest
中,更改
Ecc ecc=new Ecc();
到
然后,在SMSTest.onCreate()中执行以下操作:
ecc=new Ecc(this);
欢迎来到StackoverFlow
你的方法是错误的
活动永远不会以这种方式创建<代码>SMSTest SMSTest=新SMSTest()代码>
当您创建Ecc
对象时,最好传递对此活动的引用,然后使用该引用调用活动中的任何方法。在java类中创建静态方法,并使用类名在活动中调用该方法。反之亦然。意思是如果您想在java类中调用activity方法,请该方法在活动中是静态的,并通过活动名称调用该方法。您的问题是什么,在哪里出现错误,您可以发布您的logcat错误吗?非常感谢,但我现在对按钮有问题。我没有得到stackoverflow错误,但当我单击按钮时,它不会执行任何操作添加一些日志记录或设置断点,并查看是否调用了onClick()
方法。我检查了是否正在调用该方法,但未加载ecc类。我用更新的代码编辑了这个问题。太好了。如果我的答案有帮助,那么您可以通过单击答案旁边的复选标记来接受它。在这种情况下,许多库方法无法工作,因为它们本身不是静态的。
// Reference to the SMSTest instance
private SMSTest smsTest;
// Constructor
public Ecc(SMSTest smsTest) {
this.smsTest = smsTest;
}
Ecc ecc=new Ecc();
Ecc ecc; // This variable gets initialized in onCreate()
ecc=new Ecc(this);