Java 当我需要通知侦听器时,为什么侦听器集为空?(在Android应用程序中)
在我的Android应用程序中,我有三个类,一个是主要活动,另一个叫做CustomerTransaction,它只是一个Java类。还有另一个名为DataActivity的活动,当单击MainActivity屏幕上的“输入数据”按钮时开始。activity DataActivity允许用户在EditText中输入数字,然后单击Send按钮触发CustomerTransaction中的方法。我将在下面添加完整的代码。但首先让我解释一下我的问题。在CustomerTransaction中,我有一个方法为两个变量提供值:totalAmount和transactionType(两者都是int)。当这些值可用时,需要在主活动中显示在主屏幕上。MainActivity实现了一个名为CustomListeners的接口,该接口包含显示两个值(在TextView中)的函数头。当值可用时,通过通知侦听器,将值从CustomerTransaction传递到MainActivity中的方法。问题是当我有这些值时,侦听器集是空的。我想了解为什么会发生这种情况,然后纠正代码 代码如下:Java 当我需要通知侦听器时,为什么侦听器集为空?(在Android应用程序中),java,android,listener,Java,Android,Listener,在我的Android应用程序中,我有三个类,一个是主要活动,另一个叫做CustomerTransaction,它只是一个Java类。还有另一个名为DataActivity的活动,当单击MainActivity屏幕上的“输入数据”按钮时开始。activity DataActivity允许用户在EditText中输入数字,然后单击Send按钮触发CustomerTransaction中的方法。我将在下面添加完整的代码。但首先让我解释一下我的问题。在CustomerTransaction中,我有一个方
package com.example.customlistenertest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements CustomListeners {
TextView tv_amount;
TextView tv_transactionType;
Button btn_enterData;
CustomerTransaction control= new CustomerTransaction();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
control.registerListener(this);
tv_amount = (TextView) findViewById(R.id.textView_amount);
tv_transactionType = (TextView) findViewById(R.id.textView_transactionType);
btn_enterData = (Button) findViewById(R.id.button_enterData);
addListenerOnButtonEnterData();
}
public void addListenerOnButtonEnterData() {
btn_enterData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent =
new Intent(MainActivity.this,com.example.customlistenertest.DataActivity.class);
startActivity(intent);
}
});
}
public void updateTotal(final int transactionType, final int totalAmount) {
runOnUiThread(new Runnable() {
public void run() {
tv_amount.setText(Integer.toString(totalAmount));
tv_transactionType.setText(Integer.toString(transactionType));
}});
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume(){
super.onResume();
control.registerListener(this);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
control.unregisterListener(this);
}
} // End MainActivity
以下是DataActivity类的代码:
package com.example.customlistenertest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DataActivity extends Activity {
TextView tv_enterNumber;
EditText et_number;
Button btn_send;
CustomerTransaction transaction;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_data);
btn_send = (Button) findViewById(R.id.button_send);
et_number = (EditText) findViewById(R.id.editText_number);
}
public void send(View view) {
transaction = new CustomerTransaction();
transComplete();
}
private void transComplete() {
new Thread("TransComplete") {
public void run() {
String s_number = et_number.getText().toString();
int value = Integer.parseInt(s_number);
transaction.setState(5, value);
finish();
}
}.start();
}
} // end DataActivity
以下是CustomerTransaction类的代码:
package com.example.customlistenertest;
import java.util.HashSet;
import java.util.Set;
public class CustomerTransaction {
public static final int DATA_ENTERED = 1;
public static final int TRANSACTION_COMPLETE = 5;
private int state;
private int totalAmount;
private int transactionType;
public Set<CustomListeners> listeners = new HashSet<CustomListeners>();
public synchronized void registerListener(CustomListeners listener) {
listeners.add(listener);
}
public synchronized void unregisterListener(CustomListeners listener) {
listeners.remove(listener);
}
public synchronized void notifyListeners() {
for(CustomListeners listener : listeners) {
if (totalAmount != 0)
listener.updateTotal(transactionType,totalAmount);
else
listener.updateTotal(transactionType,0);
}
}
public void performOperation() {
new Thread("MyThread") {
public void run() {
notifyListeners();
}
}.start();
}
public void setState(final int state, final int value) {
this.state = state;
this.totalAmount = value;
this.transactionType = 999;
if (state == DATA_ENTERED) {
}
if (state == TRANSACTION_COMPLETE) {
performOperation();
}
}
} // end CustomerTransactionjava
package com.example.customlistenertest;
导入java.util.HashSet;
导入java.util.Set;
公共类CustomerTransaction{
输入的公共静态最终整数数据=1;
公共静态最终整数事务_COMPLETE=5;
私人和国家;
私人总金额;
私有int事务类型;
public Set listeners=new HashSet();
公共同步无效注册表侦听器(CustomListeners侦听器){
添加(侦听器);
}
公共同步无效注销侦听器(CustomListeners侦听器){
删除(侦听器);
}
公共同步的void notifyListeners(){
for(CustomListeners侦听器:侦听器){
如果(总金额!=0)
updateTotal(transactionType,totalAmount);
其他的
updateTotal(transactionType,0);
}
}
公共行政运作{
新线程(“MyThread”){
public void run(){
notifyListeners();
}
}.start();
}
公共无效设置状态(最终整型状态,最终整型值){
this.state=状态;
这个。总金额=价值;
this.transactionType=999;
如果(状态==输入的数据){
}
如果(状态==事务完成){
执行操作();
}
}
}//结束CustomerTransactionjava
我还将添加两个布局文件。第一个是activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView_transactionType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button_enterData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Data" />
</LinearLayout>
下一个是文件enter_data.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_enterNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter a number:" />
<EditText
android:id="@+id/editText_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
android:text="Send" />
</LinearLayout>
最后,这里是清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.customlistenertest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:theme="@style/AppTheme"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:label="@string/app_name" android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.customlistenertest.DataActivity" >
</activity>
</application>
</manifest>
MainActivity中使用的“CustomerTransaction事务”与DataActivity中定义的“CustomerTransaction事务”的实例不同。这解释了为什么DataActivity one为空,因为您只订阅了MainActivity中的一个。该问题有两种可能的解决方案:
祝你好运 “侦听器集为空”是什么意思?@codeMagic event虽然我只需要一个侦听器,但我使用哈希集将侦听器添加到其中,以便注册侦听器。请参阅ClassA中的代码。使用调试器,当我调用想要通知侦听器(s0)时,我可以看到此集为空。因此没有侦听器要通知,尽管之前我可以看到litener已添加到哈希集。我是否回答了您的问题?
if(totalAmount!=null)
是的,我试图简化代码。事实上,totalAmount是一个对象,它有一个int变量。我将编辑代码。为什么您希望当前代码正常工作?现在,您的两个活动MainActivity
和DataActivity
都有自己的CustomTransaction
对象,这意味着侦听器ou register将只应用于该对象(这不会影响其他活动)。您可能希望将CustomTransaction
类作为一个单例,以便它在两个活动之间共享其状态。