im trying to setup two listeners for one button. Here is my code:
This is the main code
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");
}
}
This is a class which i create an object in the main code :
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");
}
}
I keep getting a null pointer exception here is the logcat dump:
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)
You should use the
CompositeListenerpattern. I use it all the time, because I want to separate concerns, and keep my listeners in external classes, not anonymous.The idea is basically to have a unique listener that delegates all its
onClickcalls to all the listeners you want, which it references. The beautiful thing is that you can apply that pattern to any sort of listener (even your custom ones).(The following is quickly coded from memory in the SO editor, it may contain syntax error, but will give you an idea)
You create one of these and feed it all the listeners you want to pass to your button (including your logging listener:
And you add it to your button: