NullPointerException When Setting ClickListener on a RelativeLayout
-
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 :)
-
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 :)
-
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.It is never null when I debug. It always finds a resource.
-
It is never null when I debug. It always finds a resource.
-
It is never null when I debug. It always finds a resource.
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
-
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 :)
Bro you can not type casting of the java Programming
You can uselayoutHomeButton = (RelativeLayout)findViewById(R.id.layoutHomeButton);
And then build of your program