Calling findViewById() before setContentView() causes NullPointerException but not always? [duplicate] - android

This question already has answers here:
NullPointerException with findViewById in variable definition
(1 answer)
findViewByID returns null
(33 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
Well i get a NullPointerException when i I call findViewById() to access a Button. I think I understand why I'm getting this error but there are some questions I have unsolved in my head.
Well I think I'm getting this error because I moved the findViewById() calls from inside the onCreate() method to class scope, outside of all the methods.
So now I'm initializing my Button's and EditText's outside the onCreate() method.
Well if I understand correctly, this is happening(Null error) cause the setContentView() method is called after the findViewById() method, so that's why it throws an Exception.
But what I don't understand is that I have done the same thing in my second activity and works well without any null exception. And I'm initializing my Buttons etc outside the onCreate() method!
It does confuse me a little bit. Any help clearing this in my head would be much appreciated.
First Activity
public class FirstActivity extends AppCompatActivity {
private Button signUpButton= findViewById(R.id.lo_signUpButton);
private Button loginButton = findViewById(R.id.lo_loginButton);
private EditText username= findViewById(R.id.lo_usernameText);
private EditText password= findViewById(R.id.lo_passwordText);
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set view
setContentView(R.layout.activity_login);
Log.i(TAG,"Create "+formatter.format(new Date()));
//listeners
signUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, SignUpActivity.class));
finish();
}
});
}
Second Activity
public class SecondActivity extends AppCompatActivity {
private EditText username = findViewById(R.id.su_username);
private EditText password = findViewById(R.id.su_password);
private TextView errorText= findViewById(R.id.su_error_msg);
private Button signUpButton=findViewById(R.id.su_signupButton);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set view
setContentView(R.layout.activity_signup);
Log.i(TAG,"Create");
//listeners
Button backButton = findViewById(R.id.su_backButton);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
Log.i(TAG,"Going Back ");
finish();
}
});

You can't use this initialization:
public class SecondActivity extends AppCompatActivity {
private Button signUpButton= findViewById(R.id.lo_signUpButton);
//....
}
Use:
public class SecondActivity extends AppCompatActivity {
private Button signUpButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set view
setContentView(R.layout.activity_signup)
signUpButton= findViewById(R.id.lo_signUpButton);
//...
}
}
In the 2nd Activity you are doing something different:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
Button backButton = findViewById(R.id.su_backButton);
//...
}
and it is correct since you declaring and initializing the backButton with the findViewById method inside the onCreate after the setContentView call.
In the 1st Activity you are setting the listener with signUpButton.setOnClickListener but the signUpButton is not initialized (since it is outside the onCreate method)
Also in the 2nd Activity the other buttons that are wrongly initialized are not used in the OnCreate method.

In second activity, you have not used the buttons that are wrongly initialized, once you use them, it will cause exception too.
You have used backButton which is correctly initialized.

The answer is quite simple. the global variables get initialized before setContentView() is called, meaning what findViewById() will return null. findViewById() returns the view if found or null. As you didn't call setContentView yet, it cannot find the view you want.
The global variables in FirstActivity as well as SecondActivity are all null.
The difference between FirstActivity and SecondActivity is, that you don't access any global variable in SecondActivity.
The only view you used in SecondActivity is backButton, which you retrieved after calling setContentView, so of course, no NullPointerException will be thrown.
Always declare the variable as globally and initialize it in onCreate(). Even better, try to avoid global variables and pass them though method parameters.

Related

Button variable turns to null after calling method

In my mainactivity I have the following snip
MainActivity.class
private Button btnx10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btnx10=(Button)findViewById(R.id.MainCOPbtn);
DrawLines();
}
private void drawLines(){
float centerYOnImage1=btnx10.getHeight()/2;
}
I'm trying to access the button that is created in the onCreate() method from the method drawLines()
i.e. in the same class MainActivity.class but outside of this method.
When I am trying to access the button in the drawlines()method it's value is null.
How can I access the button?
Since you have declared the Button in Scope of Method onCreate()
Button btnx10=(Button)findViewById(R.id.MainCOPbtn);
and you are trying to access it outside of the method onCreate(), that makes it inaccessible outside of this method.
Just make the reference on class level (Globally) and use the same Reference in onCreate() method.
you can do this:-
private Button btnx10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnx10 = (Button)findViewById(R.id.MainCOPbtn);
DrawLines();
}
private void drawLines(){
float centerYOnImage1 = btnx10.getHeight()/2;
}
Change the code to
btnx10= findViewById(R.id.MainCOPbtn);
You are casting Button in the declaration which makes global variable inaccessible.
Remove local declaration of Button again.
Just use btnx10=(Button)findViewById(R.id.MainCOPbtn); in onCreate()
You are declaring Button btnx10 twice. Remove the local declaration.
You should declare outside the method, and define inside the method.
Class MainActivity...
private Button btnx10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
btnx10=(Button)findViewById(R.id.MainCOPbtn); //MINOR CORRECTION IN THIS LINE
DrawLines()
}
private void drawLines() {
float centerYOnImage1=btnx10.getHeight()/2;
}

Android: How to execute a method before the layout is initiated/created in an activity

I need to execute a method before initiating the layout in an activity. If I call the method I need to execute inside onCreate(), would it be executed before the layout is set?
The reason is because I need the method to return a piece of information that is displayed in the layout before initiating it. Would love some feedback on this.
You can do whatever you like before setContentView like so:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int i = 0;
setContentView(R.layout.main);
}
}
As long as you do not interact with views that have not been inflated yet
For example this is an error:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ERROR, CAN'T TOUCH UI ELEMENTS
ImageView img = (ImageView)findViewById(R.id.img);
setContentView(R.layout.main);
}
}
Default activity created with Android Studio contains following code
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here is code, that executed before layout inflated
setContentView(R.layout.example_activity); //This line inflates layout
}
BTW, you can even remove setContentView and inflate layout programmaticaly.
Do it in onCreate(), preferably before calling setContentView().
However, if the data you want to receive comes from the network, then it will be obtained on a separate Thread (as no network calls can be done on the main Thread). In this situation the layout will almost certainly display before the data is obtained.
A solution would be to obtain the piece of data before you start the Activity, pass it in the Intent as extra and then retrieve in onCreate() using getIntent().getStringExtra()
You are probably inflating your layout in Activity.onCreate() with setContentView(), so you need to put your function call in that method before the call to setContentView().
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
yourFunctionCallHere();
setContentView(R.layout.act_main);
}

Why do I get a NullPointerException here?

I have my code defined the way below. There are two crucial activities. Activity (1) shows some images in a ViewFlipper. It uses methods to load desired image directly. The onOptionsItemSelected() method fetches data from a menu defined within linked XML layout R.layout.browse. The other method, displaySelectedFlag(), gets a tag parameter passed from a different activity, let's call it activity (2).
Activity (1):
public class BrowserActivity extends AppCompatActivity implements SimpleGestureListener, View.OnClickListener {
public ViewFlipper vFlipper;
(...)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
vFlipper = (ViewFlipper) findViewById(R.id.viewFlipperBrowser);
(...)
} // onCreate() ends here
// this method below works fine:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
vFlipper.setDisplayedChild(item.getOrder());
return true;
}
// and this one doesn't:
public void displaySelectedFlag(int orderTag) {
vFlipper.setDisplayedChild(orderTag); // crashes here
}
}
Activity (2):
public class ListActivity extends Activity implements View.OnClickListener {
private BrowserActivity browserActivity = new BrowserActivity();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
ImageButton imageA = (ImageButton) findViewById(R.id.img_a);
imageA.setOnClickListener(this);
ImageButton imageB = (ImageButton) findViewById(R.id.img_b);
imageB.setOnClickListener(this);
}
public void displayImageInfo(View view) {
String tagValue = (String) view.getTag();
int tagId = Integer.parseInt(tagValue);
Intent intent = new Intent(this, BrowserActivity.class);
startActivity(intent);
browserActivity.displaySelectedImage(imageId);
}
#Override
public void onClick(View view) {
displayImageInfo(view);
}
}
As I checked, the method onClick() called in activity (2) fetches an ID of an ImageButton and passes it to activity (1). Unfortunately, I get a NullPointerException when calling the ViewFlipper (the line is marked in the code above, activity (1)).
Any idea why it happens?
You cannot reference one Activity from another activity. You must let the Android OS create the Activity object via the call to "startActivity". Allocating a local variable as an instance of an Activity doesn't actually mean anything (like your instantiation of the BrowserActivity). Apoorv's comment links to a decent article on the subject.
If you want to pass data from one Activity to another, you need to pass extras within the Intent's bundle. This post goes into detail: https://stackoverflow.com/a/819427/504252

How do I display public text in Activity A after it has been changed by Activity B

This probably demonstrates the most appalling lack of understanding of the activity life cycle, but please be sympathetic. I am ultimately going to want to invoke Activity B from Activity A a number of times, each time passing a different parameter to Activity B which is then responded to by the user and stores/sets various public variables. As a precursor to this, I just want to get my head round how Activity A sees the change to a public variable that Activity B has changed.
I have three very simple classes: Common.java that holds the public variables, the main activity MainActivity.java and the child activity Child.java. There is only one public variable right now; it's the string mess1 which is initialized to "***". All the code does at the moment is when mainbutton is clicked in MainActivity, it invokes Child. In Child, we immediately set mess1 to "Child here" then set the text in a Child-based TextView to mess1. On clicking the childbtn button in Child, we finish() the child activity (and of course the system returns us to MainActivity.
When this app is run, wee see the three stars displayed in MainActivity. When mainbutton is pressed we go to Child and see "Child here" displayed. When the childbtn is pressed, we return to MainActivity BUT, the three stars are still there although we know for sure that mess1 now holds "Child here".
My questions are:
1. Why, when we know mess1 has been changed, does MainActivity still display "***" on return from the Child activity?
2. What do I need to change in the code to get "Child here" to display?
Relevant code extracts follow. Thanks in advance for your help.
Common.java
public class Common
{
public static String mess1 = "***";
}
MainActivity.java
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainbutton = (Button)findViewById(R.id.mainbutton);
TextView maintop = (TextView)findViewById(R.id.maintop);
mainbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
startActivity(new Intent(MainActivity.this, Child.class));
}
});
maintop.setText(Common.mess1);
}
Child.java
public class Child extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
TextView childtext = (TextView) findViewById(R.id.childtext);
final Button childbtn = (Button) findViewById(R.id.childbtn);
Common.mess1 = "Child here";
childtext.setText(Common.mess1);
childbtn.setOnClickListener
(new View.OnClickListener()
{public void onClick(View v)
{finish();
}
}
);
}
Likely you are moving back on the back stack history and you are resuming the previous activity that was placed in a paused state and therefore the onCreate isn't being called but the onResume (of the initial activity)..
Using global state this way isn't advised but this should work if you place the appropriate code in the onResume method.
You should set the text in onResume() of MainActivity. When you get back from Child.java onResume() (not onCreate()) is invoked and, since maintop's text is set in onCerate() only, nothing changes it on return.
protected void onResume() {
super.onResume();
maintop.setText(Common.mess1);
}
Reference: Activity Lifecycle and Implementing the lifecycle callbacks

Android application closes on click

I do my android app , but I have a bug I don't know how to fix it.
My code is below :
This is my Main Activity :
public class MainActivity extends Activity {
private ImageView imgHot;
public final static String EXTRA_MESSAGE="com.cuonglm.KhoHinh.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgHot=(ImageView)findViewById(R.id.imageViewHot);
imgHot.setOnClickListener(toContentHot);
}
View.OnClickListener toContentHot=new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent content=new Intent(MainActivity.this,ContentActivity.class);
String signal="1";
content.putExtra(EXTRA_MESSAGE,signal);
startActivity(content);
}
};
And this is my second Activity :
public class ContentActivity extends Activity {
private TextView viewMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
setContentView(R.layout.activity_content);
}
I want to click on the image on the Main Activity , string "1" or number "1" will send to the Second Activity via Intent and view on the TextView.
But my app will be close "Unfortunately..."
Thanks
Change to
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
In your ContentActivity
findViewById looks for a view with the id for the current infalted layout. SO you need to set the content of your layout to the activity first and then initialize your views.
You are probably gettting NullPointerException coz your initialization fails.
You need to call setContentView() in your second Activity before trying to access any of the Views in that layout. Change it to
public class ContentActivity extends Activity {
private TextView viewMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
}
If this doesn't fix your problem then please post your logcat so we can see the error. Also always post logcat in the future when your app crashes. They aren't always this easy to see.
Also, I'm not sure you understand how putExtra() works. It is a key, value pair so when you put EXTRA_MESSAGE as the key then that is what you would use to retrieve the value added in the second param. So the way you are doing it may work if the Activity gets destroyed but it looks really strange to me and probably not realy safe or efficient. I would change it to something like
content.putExtra("message",signal);
in your first Activity then get it with
String messageReceive = content.getStringExtra("message");
in your second Activity
You need to set the layout before trying to reference the Views associated with it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_content);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
}

Categories

Resources