This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
findViewById returns null
(4 answers)
Closed 4 years ago.
I'm trying to set the onClickListener on an ImageButton but I get the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bordi.hotel, PID: 18420
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bordi.hotel/com.example.bordi.hotel.DetaiImage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.bordi.hotel.DetaiImage.onCreate(DetaiImage.java:28)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
Below is the code I'm using:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
close = (ImageButton) findViewById(R.id.closeDetail);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DetaiImage.super.onBackPressed();
}
});
}
Thanks in advance for your help!
You forgot setContentView(R.layout.activity_main);
after super.onCreate(savedInstanceState);
and if you have not a prior global declaration in your activity of close, do it like this:
ImageButton close = (ImageButton) findViewById(R.id.closeDetail);
Related
I couldn't find anything that was not initialized, and can't work my way up further. The same bit works for another piece of code that I've written, both are totally identical, and I can't figure out the problem. The complete error code is -
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
The log file-
2021-07-30 21:29:33.613 17989-17989/com.elc.hostel E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elc.hostel, PID: 17989
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elc.hostel/com.elc.hostel.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.elc.hostel.LoginActivity.onCreate(LoginActivity.java:50)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-07-30 21:29:35.004 17989-18005/com.elc.hostel W/System: A resource failed to call close.
The code-
public class LoginActivity extends AppCompatActivity {
EditText mEmail, mPassword;
Button mLogin;
ProgressBar mProgressBar;
FirebaseAuth fAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Objects.requireNonNull(getSupportActionBar()).hide();
mEmail = findViewById(R.id.LoginEmail);
mPassword = findViewById(R.id.LoginPassword);
mLogin = findViewById(R.id.LoginButton);
mProgressBar = findViewById(R.id.progressBarLog);
fAuth = FirebaseAuth.getInstance();
mLogin.setOnClickListener(new View.OnClickListener() {------------------Error
#Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)){
mEmail.setError("Email is required");
return;
}
if (TextUtils.isEmpty(password)){
mPassword.setError("Password is required");
return;
}
if (password.length() < 6){
mPassword.setError("Password must be at least 6 characters");
return;
}
mProgressBar.setVisibility(View.VISIBLE);
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.elc.hostel.LoginActivity.onCreate(LoginActivity.java:50)
Above stack indicates that mLogin is null.
Few cases in which findViewById() returns null are :
findViewById() is called before setContentView().
id passed in findViewById() is not present in layout set in setContentView().
In this case, setContentView() is not called.
Add setContentView() before accessing the views will solve the problem.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
When the register button is clicked the app should load another activity however it just crashes. Tried looking here and here for help but I still couldn't fix it. I also tried removing 'implements View.OnClickListner' and using it how its shown in the examples and it didn't work. Also tried changing the xml file to a LinearLayout and that didn't work either. Thanks in advance!
public class LogInActivity extends AppCompatActivity implements View.OnClickListener {
//user input variables
EditText emailAddressInput;
EditText passwordInput;
Button logInButton;
Button registerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set the layout to activity_login.xml
setContentView(R.layout.activity_login);
//get user inputs and set to the variables
emailAddressInput = (EditText)findViewById(R.id.emailAddressInput);
passwordInput = (EditText)findViewById(R.id.passwordInput);
logInButton = (Button)findViewById(R.id.logInButton);
registerButton = (Button)findViewById(R.id.registerButton);
logInButton.setOnClickListener(this);
registerButton.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent registerIntent = new Intent (LogInActivity.this,RegisterActivity.class);
switch (view.getId())
{
case R.id.logInButton:
break;
case R.id.registerButton:
startActivity(registerIntent);
break;
}
}
}
EDIT:
the logcat:
2020-04-04 16:02:10.759 12718-12718/com.example.blooddonorsystem E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.blooddonorsystem, PID: 12718
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blooddonorsystem/com.example.blooddonorsystem.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at com.example.blooddonorsystem.RegisterActivity.onCreate(RegisterActivity.java:56)
at android.app.Activity.performCreate(Activity.java:7825)
at android.app.Activity.performCreate(Activity.java:7814)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
In your RegisterActivity, seems like the Spinner you are setting the adapter to is null. Make sure it has correct value, usually its an ArrayAdapter of String.
The error is not in LoginActivity but in RegisterActivity.There is a null pointer exception in RegisterActivity when you are setting adapter to a spinner.
ERROR
**2020-03-01 17:36:58.959 6589-6589/com.studenthelper.bscit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.studenthelper.bscit, PID: 6589
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.studenthelper.bscit/com.studenthelper.bscit.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4694)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
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:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
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:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:7140)
at android.app.Activity.performCreate(Activity.java:7131)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4694)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
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:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
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:858) **
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
TextView textView=findViewById(R.id.logotext);
int unicode=0x1F4A1;
String emoji=getEmoji(unicode);
String Text="Bsc"+emoji+"T";
textView.setText(Text);
}
public String getEmoji(int uni)
{
return new String(Character.toChars(uni));
}
As per your crash logs:
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at
com.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:20)
It means your textView
TextView textView=findViewById(R.id.logotext);
Is not found from the layout R.layout.activity_main due to which when you try to textView.setText(Text); it's actually null and exception occurs.
So, I'll suggest to
Verify textVIew Id is same in the layout? and
Set the getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
before the setContentView(R.layout.activity_main);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView textView=findViewById(R.id.logotext);
int unicode=0x1F4A1;
String emoji=getEmoji(unicode);
String Text="Bsc"+emoji+"T";
textView.setText(Text);
}
To summarise the problem: You have defined two layouts, which one of them does not have a TextView. From your code obviously the crash is caused by calling .setText() on a null object.
The best way is always to add back the missing TextView back to your another layout, so all your layouts contains the same set of IDs. This can avoid bugs caused by findViewById(). In the layout which you do not need the TextView, you can simply set its android:visibility="gone" to hide it.
Alternatively back to your code, if you know the TextView is not always there, you can simply check if your textView variable is null or not, before calling .setText(). This can also avoid the NullPointerException.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm trying to findViewById in constructor in Login class but it throws a error on object create and crash application.
Error throws only on construct in Login class not on listener.
After button clicked it should call loginAction().
What should i do to fix that (this is my first app) ?
Error:
11-26 09:13:57.592 5926-5926/com.wolfriders E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wolfriders, PID: 5926
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wolfriders/com.wolfriders.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2563)
at com.wolfriders.Login.<init>(Login.java:13)
at com.wolfriders.Main.onCreate(Main.java:18)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Main:
package com.wolfriders;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main extends AppCompatActivity {
private boolean logged = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (logged == false) {
setContentView(R.layout.login_from);
final Login login = new Login();
login.login_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login.loginAction();
}
});
} else { setContentView(R.layout.main_activity); }
}
}
Login:
package com.wolfriders;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
public class Login extends Activity {
public EditText login_phone, login_password;
public Button login_btn;
Login() {
this.login_phone = (EditText)findViewById(R.id.inputloginpassword);
this.login_password = (EditText)findViewById(R.id.inputloginpassword);
this.login_btn = (Button)findViewById(R.id.btnlogin);
}
public void loginAction() {
// Do something after login button clicked
}
}
If your Login Class needs to be an Activity you would need to override onCreate function since it is activity and then you need an .xml file to associate that activity class with ui represented. How did you make this Login class (I mean as an activity or just a class)? On more detailed reading and explanation about activities you can read here:
https://developer.android.com/guide/components/activities/intro-activities.html
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
When clicking a Button to switch from the main Activity to my "ColoursGame" Activity the app crashes.
I'm still a beginner so not an expert at debugging.
The main activity code
Button colorsGame = (Button) findViewById(R.id.colours);
colorsGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ColoursGame.class));
}
});
Manifest new activity
<activity android:name=".ColoursGame"
android:label="ColourGame"
android:theme="#style/AppTheme.NoActionBar"></activity>
ColoursGame Activity OnCreate code
public class ColoursGame extends Activity {
int livesCount = 3;
String x;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_colours_game);
Button start = (Button) findViewById(R.id.startColors);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vSwitch.showNext();
x = String.valueOf(livesCount);
lives.setText(x);
text();
textColor();
backgroundColor();
start();
}
});
}
The Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aynaar.numbersgameforkids, PID: 3278
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aynaar.numbersgameforkids/com.aynaar.numbersgameforkids.ColoursGame}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2323)
at com.aynaar.numbersgameforkids.ColoursGame.<init>(ColoursGame.java:42)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
at com.aynaar.numbersgameforkids.ColoursGame.<init>(ColoursGame.java:42)
Don't call findViewById outside of onCreate, there's no Window to call findViewById at that point.
If you still get a NullPointer, then you must have #+id/startColors in activity_colours_game.xml
Answer explanation here
Button start = (Button) findViewById(R.id.startColors);
According to the error you are trying to reach a button that doesnt exist in your activity layout file (whatever it is). Check the id of your button and make sure it is placed on the layout page that related with your "ColoursGame" Activity.