Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
Instance member variables are initialized when the instance itself is initialized. It's too early for findViewById()
Before onCreate()
your activity does not yet have a Window
that findViewById()
needs internally. Before setContentView()
(that you should be calling in onCreate()
) there are no views to be found either.
Therefore init your view references in onCreate()
and after setContentView()
.
App crashes when member variables are assigned with value
You cannot use findViewById()
unless you actually set a layout to the Activity
. Your app will crash because when the class is created first, there is no Button
with ID R.id.buttonTrue
as you've not set the layout.
You'll always get NullPointerException
with what you're doing.
Correct way of finding the Views by ID is after they're available in the Activity view hierarchy after the setContentView(R.layout.activity_main)
is executed.
You'll encounter the same NullPointerException
if you try to find the same view before setContentView()
is executed, i.e. if you try to do this.
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = findViewById(R.id.your_button);
//This will give NullPointerException as you're trying to find
//button before activity gets any layout to work with
setContentView(R.layout.activity_main);
}
App crahing when Button declared outside onCreate method
If you want a reference to your button globally
then you should declare it outside then initialize it in onCreate
Button b1; //declare the button variable
public class SharedPreferencesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
b1 = (Button) findViewById(R.id.buttonSave); //initialize on creation
}
You should read more into the basics of Java
when learning Android
App crashes while trying to get my Camera IDs
you need to initialise views inside the Activity onCreate method. Before setContentView is called you cannot look up views by id.
So replace this:
public TextView text = (TextView)findViewById(R.id.text);
with:
public TextView text;
//....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
text = (TextView)findViewById(R.id.text);
//...
Why does my app crash even when there is no error (HELP)
You should declare the RadioButtons as null in a first step :
RadioButton AnswerA_ID = null;
RadioButton AnswerB_ID = null;
RadioButton AnswerC_ID = null;
RadioButton AnswerD_ID = null;
Then in the OnCreate you should do :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AnswerA_ID = (RadioButton) findViewById(R.id.AnswerA);
AnswerB_ID = (RadioButton) findViewById(R.id.AnswerB);
AnswerC_ID = (RadioButton) findViewById(R.id.AnswerC);
AnswerD_ID = (RadioButton) findViewById(R.id.AnswerD);
}
Related Topics
Android Accelerometer Accuracy (Inertial Navigation)
Determining the Current Foreground Application from a Background Task or Service
Maintain/Save/Restore Scroll Position When Returning to a Listview
How to Open the Google Play Store Directly from My Android Application
How to Update Recyclerview Adapter Data
Open Another Application from Your Own (Intent)
Using Application Context Everywhere
How to Detect When an Android App Goes to the Background and Come Back to the Foreground
Sending Data Back to the Main Activity in Android
How to Get Current Time and Date in Android
What to Do on Transactiontoolargeexception
How to Send Emails from My Android Application
How to Detect Incoming Calls, in an Android Device
Detect If Android Device Has Internet Connection
Can't Find Theme.Appcompat.Light For New Android Actionbar Support