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 9260799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:59:08+00:00 2026-06-18T12:59:08+00:00

I have a problem implementing a button listener in a separate class. The code:

  • 0

I have a problem implementing a button listener in a separate class. The code:

<Button 
    android:id ="@+button/bottone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text = "Button"
    />

the main class:

public class MainActivity extends Activity {

private final Context main_context = this.getApplicationContext(); 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Button b = (Button)findViewById(R.button.bottone);
    Log.i("AAAAAAAAAAAA", "1");

    OnClickListener l = new MyClickListener(this);
    b.setOnClickListener(l);
    Log.i("AAAAAAAAAAAA", "2");


    setContentView(R.layout.activity_main);
}

public Context getContext() {
    return main_context;
}

and the implementation of listener

public class MyClickListener implements OnClickListener {
private final Context cntx;

public MyClickListener(Context c) {
    cntx = c;
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Log.i("ONCLICK", "bottone cliccato");
    Toast t = Toast.makeText(cntx, "OK", Toast.LENGTH_LONG);
    t.show();
}

Now when i try to run it, application crash on my phone, and logcat tolds

unable to instantiate activity componentinfo
on mainactivity.

what’s wrong?

the log of logcat info

02-07 11:43:54.887: E/AndroidRuntime(9048): FATAL EXCEPTION: main
02-07 11:43:54.887: E/AndroidRuntime(9048): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.buttoneventlistener/com.example.buttoneventlistener.MainActivity}: java.lang.NullPointerException
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.os.Looper.loop(Looper.java:137)
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 11:43:54.887: E/AndroidRuntime(9048): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 11:43:54.887: E/AndroidRuntime(9048): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 11:43:54.887: E/AndroidRuntime(9048): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-07 11:43:54.887: E/AndroidRuntime(9048): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-07 11:43:54.887: E/AndroidRuntime(9048): at dalvik.system.NativeStart.main(Native Method)
02-07 11:43:54.887: E/AndroidRuntime(9048): Caused by: java.lang.NullPointerException
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
02-07 11:43:54.887: E/AndroidRuntime(9048): at com.example.buttoneventlistener.MainActivity.(MainActivity.java:13)
02-07 11:43:54.887: E/AndroidRuntime(9048): at java.lang.Class.newInstanceImpl(Native Method)
02-07 11:43:54.887: E/AndroidRuntime(9048): at java.lang.Class.newInstance(Class.java:1319)
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
02-07 11:43:54.887: E/AndroidRuntime(9048): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
02-07 11:43:54.887: E/AndroidRuntime(9048): … 11 more

  • 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-06-18T12:59:09+00:00Added an answer on June 18, 2026 at 12:59 pm

    move setContentView(R.layout.activity_main); line before accessing button from UI as :

    public class MainActivity extends Activity {
    
    private  Context main_context; 
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);  //<< set Activity layout here
    
        main_context = this.getApplicationContext(); 
        Button b = (Button)findViewById(R.id.bottone);
        Log.i("AAAAAAAAAAAA", "1");
    
        OnClickListener l = new MyClickListener(this);
        b.setOnClickListener(l);
        Log.i("AAAAAAAAAAAA", "2");
    
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a command button (implementing the ICommand). I want, when
i have a problem with implementing a Like-Button in a Phonegap Application for iOS.
I have a slight problem implementing vibration functionality in my application. My code is:
We have a strange problem when implementing sessions with ColdFusion in IE6. After login
I'm new in Android and I'm implementing a small project in which, I have
I'm implementing a small feature and kind of stuck. I have a button to
I have the following problem in swing. I'm implementing basic drawing operations (lines, shapes).
i have got some problem on implementing Django sessions. I have an employee listing
I'm implementing a click button for my animation plugin, but I found a problem.
I am running problems in implementing LIKE in PDO I have this query: $query

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.