Mike G
Mike G

Reputation: 4959

One button two listeners in java

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)

Upvotes: 3

Views: 2191

Answers (3)

Guillaume
Guillaume

Reputation: 22822

You should use the CompositeListener pattern. 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 onClick calls 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)

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);
        }
    } 
}

You create one of these and feed it all the listeners you want to pass to your button (including your logging listener:

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

And you add it to your button:

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

Upvotes: 9

user1021229
user1021229

Reputation: 179

Ok one possible sollution would be that on button click to create 2 different threads and let each thread do one of the actions.

Second possible sollution is create the so called Logger class with main method and and implement it on some port of your computer,so when somone clicks the button the class is called through a socket.You can make it open along with a program by using few lines of code

Third possible solution is to create RMI class that will do the logging and send back the result that will be written to a file

Upvotes: 1

Houcine
Houcine

Reputation: 24181

you can't instanciate an Activity with new, you should use startActivity(Intent intent); to launch an activity , and the second thing , is that you should declare it on your manifest file like this : <activity android:name="TempActivity" />

NB : in your activity TempActivity , you should override the method onCreate() to init your Views etc...

Upvotes: 1

Related Questions