Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6812119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:24:37+00:00 2026-05-26T20:24:37+00:00

im trying to setup two listeners for one button. Here is my code: This

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:24:37+00:00Added an answer on May 26, 2026 at 8:24 pm

    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);    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to setup two date pickers like this http://jqueryui.com/demos/datepicker/#date-range . But if
I'm trying to setup Eclipse for two monitors (a 1600x1200). I have one big
I'm trying to setup a bidirectional localConnection (LC) between two flash objects on the
I'm working with an existing database schema, and trying to setup two Doctrine models
I've searched for two days trying to find a solution to this. My problem
I have two databases, on two separate SQL Servers (trying to consolidate both). This
I am trying to setup Dozer to perform a complex mapping between my two
I've Got two Programs (Server / Client) I'm trying to setup IPC for them
I am trying to set up ehcache replication as documented here: http://ehcache.sourceforge.net/EhcacheUserGuide.html#id.s22.2 This is
I'm trying to setup a search page which performs two separate kinds of search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.