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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:18:31+00:00 2026-05-18T21:18:31+00:00

When I comment out calc1.setOnClickListener(buttonListener); this runs fine, when I allow it to run,

  • 0

When I comment out calc1.setOnClickListener(buttonListener); this runs fine, when I allow it to run, it crashes my script. How can I figure out what’s going on? Logcat shows a bunch of red, but I’m not sure how to read it…

package com.example.contactwidget;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ContactWidget extends Activity {

    private static final int HELLO_ID = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button calc1 = (Button) findViewById(R.id.calc_button_1);

        OnClickListener buttonListener = new View.OnClickListener() {
          public void onClick(View v) {

          }
        };

        calc1.setOnClickListener(buttonListener);

        setContentView(R.layout.main);
    }
}

update

Stack trace…

E/AndroidRuntime(  339): FATAL EXCEPTION: main
E/AndroidRuntime(  339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactwidget/com.example.contactwidget.ContactWidget}: java.lang.NullPointerException
E/AndroidRuntime(  339):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  339):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  339):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  339):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  339):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  339):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  339):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  339):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  339):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  339):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  339):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  339):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  339): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  339):    at com.example.contactwidget.ContactWidget.onCreate(ContactWidget.java:41)
E/AndroidRuntime(  339):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  339):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  339):    ... 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-05-18T21:18:32+00:00Added an answer on May 18, 2026 at 9:18 pm

    One thing is that you call setContentView(R.layout.main); at the end.
    You have to set the content view as above before referencing any views in a xml layout

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Button calc1 = (Button) findViewById(R.id.calc_button_1);
    
            OnClickListener buttonListener = new View.OnClickListener() {
              public void onClick(View v) {
    
              }
            };
    
            calc1.setOnClickListener(buttonListener);
        }
    

    Since you try to find a view by id without specifying any layout before (with setContentView(R.layout.main);), it returns a null object of calc1 .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this, when I try to run my PHP. When I comment out
I would like to comment out all script tags from an HtmlDocument. This way
This query runs when I comment out the code above '}else{'. Have done so
Can anyone comment out this code to give me a better idea of whats
The following view crashes the iPhone If I comment out the using (webView =
How do I comment out a block of tags in XML? I.e. How can
How can I comment out parts of a Spark view so they aren't rendered
I am having a strange problem and I can't seem to figure it out.
If I comment this block out, then the GC spam goes away. What's causing
I'm very annoyed with this error : If I comment out the require_once'../class/myclass.class.php'; the

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.