gpt4 book ai didi

java - java中的一键二监听

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:29 25 4
gpt4 key购买 nike

我正在尝试为一个按钮设置两个监听器。这是我的代码:这是主要代码

            public class LoggingEventsActivity extends Activity  implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

temp obj = new temp(R.id.button1,this);

View continueButton = findViewById(R.id.button1);
continueButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

final TextView t=(TextView)findViewById(R.id.editText1);
t.setText("hello world");

}
}

这是我在主代码中创建一个对象的类:

            public class temp extends Activity implements OnClickListener{


public temp(int id, LoggingEventsActivity ref){
try{

View continueButton = findViewById(id);
continueButton.setOnClickListener(ref);
}
catch (Exception e){
e.printStackTrace();
}
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final TextView t=(TextView)findViewById(R.id.editText1);
t.append("extra");
}

}

我不断收到空指针异常,这里是 logcat 转储:

            11-15 11:08:29.589: W/System.err(24268): java.lang.NullPointerException
11-15 11:08:29.605: W/System.err(24268): at android.app.Activity.setContentView(Activity.java:1835)
11-15 11:08:29.616: W/System.err(24268): at events.log.temp.<init>(temp.java:15)
11-15 11:08:29.635: W/System.err(24268): at events.log.LoggingEventsActivity.onCreate(LoggingEventsActivity.java:17)
11-15 11:08:29.655: W/System.err(24268): at android.app.Activity.performCreate(Activity.java:4465)
11-15 11:08:29.664: W/System.err(24268): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-15 11:08:29.675: W/System.err(24268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-15 11:08:29.685: W/System.err(24268): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-15 11:08:29.696: W/System.err(24268): at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-15 11:08:29.704: W/System.err(24268): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-15 11:08:29.715: W/System.err(24268): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 11:08:29.725: W/System.err(24268): at android.os.Looper.loop(Looper.java:137)
11-15 11:08:29.735: W/System.err(24268): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-15 11:08:29.744: W/System.err(24268): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:08:29.766: W/System.err(24268): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 11:08:29.784: W/System.err(24268): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-15 11:08:29.784: W/System.err(24268): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-15 11:08:29.795: W/System.err(24268): at dalvik.system.NativeStart.main(Native Method)
11-15 11:08:37.355: D/gralloc_goldfish(24268): Emulator without GPU emulation detected.
11-15 11:09:34.365: D/dalvikvm(24268): Debugger has detached; object registry had 441 entries
11-15 11:14:20.436: W/System.err(24548): java.lang.NullPointerException
11-15 11:14:20.445: W/System.err(24548): at android.app.Activity.findViewById(Activity.java:1794)
11-15 11:14:20.445: W/System.err(24548): at events.log.temp.<init>(temp.java:15)
11-15 11:14:20.445: W/System.err(24548): at events.log.LoggingEventsActivity.onCreate(LoggingEventsActivity.java:17)
11-15 11:14:20.455: W/System.err(24548): at android.app.Activity.performCreate(Activity.java:4465)
11-15 11:14:20.455: W/System.err(24548): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-15 11:14:20.455: W/System.err(24548): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-15 11:14:20.455: W/System.err(24548): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-15 11:14:20.455: W/System.err(24548): at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-15 11:14:20.455: W/System.err(24548): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-15 11:14:20.455: W/System.err(24548): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 11:14:20.465: W/System.err(24548): at android.os.Looper.loop(Looper.java:137)
11-15 11:14:20.465: W/System.err(24548): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-15 11:14:20.465: W/System.err(24548): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:14:20.475: W/System.err(24548): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 11:14:20.475: W/System.err(24548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-15 11:14:20.475: W/System.err(24548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-15 11:14:20.475: W/System.err(24548): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您应该使用 CompositeListener 模式。我一直在使用它,因为我想分离关注点,并让我的听众留在外部类中,而不是匿名的。

这个想法基本上是拥有一个唯一的监听器,将其所有onClick 调用委托(delegate)给您想要的所有监听器,它引用了这些监听器。美妙之处在于您可以将该模式应用于任何类型的听众(甚至是您自定义的听众)。

(以下是在SO编辑器中凭内存快速编码,可能有语法错误,但会给你一个思路)

public class CompositeOnClickListener implements OnClickListener {
private final Set<OnClickListener> delegates = new HashSet<OnClickListener>();

public CompositeOnClickListener(OnClickListener... listeners) {
for (OnClickListener listener : listeners) {
delegates.add(listener);
}
}

@Override
public void onClick(View v) {
for (OnClickListener listener : delegates) {
listener.onClick(v);
}
}
}

您创建其中一个并将其提供给您想要传递给按钮的所有监听器(包括您的日志记录监听器:

OnClickListener myListener = new CompositeOnClickListener(listener1, listener2, listener3);

然后将它添加到按钮中:

Button continueButton = (Button) findViewById(R.id.button1);
continueButton.setOnClickListener(myListener);

关于java - java中的一键二监听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8139478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com