Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. NullPointerException When Setting ClickListener on a RelativeLayout

NullPointerException When Setting ClickListener on a RelativeLayout

Scheduled Pinned Locked Moved Android
c++javaandroidcomdesign
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    Hello there. I am setting a click listener on a relative layout. It works fine. But today I got couple of crashes on this click listener from the user. I set this click listener in the onCreate() function of the activity. Here is what my code looks like

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    layoutHomeButton = findViewById(R.id.layoutHomeButton);
    layoutHomeButton.setOnClickListener(new View.OnClickListener() { // ** <===exception here**
    @Override
    public void onClick(View view) {
    // my code goes here
    }
    });
    }

    And following is the exception I get

    java.lang.RuntimeException:
    at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3021)
    at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3156)
    at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1864)
    at android.os.Handler.dispatchMessage (Handler.java:106)
    at android.os.Looper.loop (Looper.java:205)
    at android.app.ActivityThread.main (ActivityThread.java:6991)
    at java.lang.reflect.Method.invoke (Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:884)
    Caused by: java.lang.NullPointerException:
    at com.hiclass.earthlivecam.publiccam.earthcam.webcamhd.ui.activities.ActivityPlayVideo.onCreate (ActivityPlayVideo.java:210)
    at android.app.Activity.performCreate (Activity.java:7159)
    at android.app.Activity.performCreate (Activity.java:7150)
    at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3001)

    what am I doing wrong? Thanks :)

    L P 2 Replies Last reply
    0
    • D Django_Untaken

      Hello there. I am setting a click listener on a relative layout. It works fine. But today I got couple of crashes on this click listener from the user. I set this click listener in the onCreate() function of the activity. Here is what my code looks like

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      layoutHomeButton = findViewById(R.id.layoutHomeButton);
      layoutHomeButton.setOnClickListener(new View.OnClickListener() { // ** <===exception here**
      @Override
      public void onClick(View view) {
      // my code goes here
      }
      });
      }

      And following is the exception I get

      java.lang.RuntimeException:
      at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3021)
      at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3156)
      at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78)
      at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108)
      at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68)
      at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1864)
      at android.os.Handler.dispatchMessage (Handler.java:106)
      at android.os.Looper.loop (Looper.java:205)
      at android.app.ActivityThread.main (ActivityThread.java:6991)
      at java.lang.reflect.Method.invoke (Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
      at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:884)
      Caused by: java.lang.NullPointerException:
      at com.hiclass.earthlivecam.publiccam.earthcam.webcamhd.ui.activities.ActivityPlayVideo.onCreate (ActivityPlayVideo.java:210)
      at android.app.Activity.performCreate (Activity.java:7159)
      at android.app.Activity.performCreate (Activity.java:7150)
      at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1272)
      at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3001)

      what am I doing wrong? Thanks :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      layoutHomeButton = findViewById(R.id.layoutHomeButton);

      It may be that the above line failed, so layoutHomeButton is null and causes the exception. Use the debugger to see if that is the case.

      D 1 Reply Last reply
      0
      • L Lost User

        layoutHomeButton = findViewById(R.id.layoutHomeButton);

        It may be that the above line failed, so layoutHomeButton is null and causes the exception. Use the debugger to see if that is the case.

        D Offline
        D Offline
        Django_Untaken
        wrote on last edited by
        #3

        It is never null when I debug. It always finds a resource.

        L D 2 Replies Last reply
        0
        • D Django_Untaken

          It is never null when I debug. It always finds a resource.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Then something else must be the problem, but it is impossible to guess what. You need to look at ActivityPlayVideo.java:210 if it is available.

          1 Reply Last reply
          0
          • D Django_Untaken

            It is never null when I debug. It always finds a resource.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Then you should be able to add:

            layoutHomeButton = findViewById(R.id.layoutHomeButton);
            if (layoutHomeButton != null)
            layoutHomeButton.setOnClickListener(new View.OnClickListener()...

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            1 Reply Last reply
            0
            • D Django_Untaken

              Hello there. I am setting a click listener on a relative layout. It works fine. But today I got couple of crashes on this click listener from the user. I set this click listener in the onCreate() function of the activity. Here is what my code looks like

              @Override
              protected void onCreate(Bundle savedInstanceState) {
              layoutHomeButton = findViewById(R.id.layoutHomeButton);
              layoutHomeButton.setOnClickListener(new View.OnClickListener() { // ** <===exception here**
              @Override
              public void onClick(View view) {
              // my code goes here
              }
              });
              }

              And following is the exception I get

              java.lang.RuntimeException:
              at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3021)
              at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3156)
              at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78)
              at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108)
              at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68)
              at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1864)
              at android.os.Handler.dispatchMessage (Handler.java:106)
              at android.os.Looper.loop (Looper.java:205)
              at android.app.ActivityThread.main (ActivityThread.java:6991)
              at java.lang.reflect.Method.invoke (Native Method)
              at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
              at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:884)
              Caused by: java.lang.NullPointerException:
              at com.hiclass.earthlivecam.publiccam.earthcam.webcamhd.ui.activities.ActivityPlayVideo.onCreate (ActivityPlayVideo.java:210)
              at android.app.Activity.performCreate (Activity.java:7159)
              at android.app.Activity.performCreate (Activity.java:7150)
              at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1272)
              at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3001)

              what am I doing wrong? Thanks :)

              P Offline
              P Offline
              Pratishkumar Kushwaha
              wrote on last edited by
              #6

              Bro you can not type casting of the java Programming
              You can use

              layoutHomeButton = (RelativeLayout)findViewById(R.id.layoutHomeButton);

              And then build of your program

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups