App crahing when Button declared outside onCreate method [duplicate] - android

This question already has answers here:
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
(9 answers)
Closed 1 year ago.
The following code crashes the app
public class SharedPreferencesActivity extends AppCompatActivity {
Button b1=(Button)findViewById(R.id.buttonSave); //**ERROR**
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
}
Whereas the following code works fine
public class SharedPreferencesActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
Button b1=(Button)findViewById(R.id.buttonSave);//**REPLACED BUTTON DECLARATION**
}
Why is this happening?

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

Related

why am i getting error message on MainActivity Java?

I always get an error warning anytime i want to set find view by id
public class MainActivity extends AppCompatActivity {
View pdfView = findViewById(R.id.pdfView);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView.**fromAsset**("").load();
}
The error comes from "fromAsset." It always highlighted in red.
Does anyone have a solution to this?
Please initialize View inside onCreate() Method.
View pdfView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView = findViewById(R.id.pdfView);
pdfView.**fromAsset**("").load();
}

Why must be objects initialized in onCreate?

I have the following code:
public class MainActivity extends Activity {
TextView number=(TextView)findViewById(R.id.number2);
TextView number2=(TextView)findViewById(R.id.number2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number.setText("Text");
number.setText("Text");
}
followed by more code, but when I run it it crashes.
After doing that i tried to initialize TextViews in onCreate()
public class MainActivity extends Activity {
TextView number;
TextView number2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number=(TextView)findViewById(R.id.number);
number2=(TextView)findViewById(R.id.number2);
number.setText("Text");
number.setText("Text");
}
and it worked. Why must objects be initialized in onCreate?
Your activity won't have a Window until onCreate(). Attempting to find any views before the window is initialized will lead to NPE.
Additionally, attempting to find views before setContentView() will return nulls and so the return values are not good for anything.

Crash on using onResume method [duplicate]

This question already has answers here:
I would like to set my variables at the top of my class instead of in the method
(4 answers)
Closed 7 years ago.
What's wrong with the following code?
I'm trying to use onResume method but it's crashing.
The IDs in the XML are correct.
public class MainActivity extends ActionBarActivity {
TextView ford = (TextView) findViewById(R.id.krux);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
ford.setText("camphor");
}
}
Use the findViewById after setting the layout. Currently you try to initialize the textview before you even set it.
public class MainActivity extends ActionBarActivity {
TextView ford;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ford= (TextView) findViewById(R.id.krux);
}
#Override
protected void onResume() {
super.onResume();
ford.setText("camphor");
}
}

Application didn't launch when I create a Button in java

I've a strange problem. I explain after this :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// final Button buttonAlpha = (Button) findViewById(R.id.Alphabet);
}
Look you see, I put in comments the buttonAlpha and the launching of application works perfectly but when I remove the comments, my app didn't launching and I've no idea why. If somebody have/had the same problem, can I help me please ?
You need to put
final Button buttonAlpha = (Button) findViewById(R.id.Alphabet);
inside a method or else it will try to run it before running onCreate(), hence before setContentView()
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonAlpha = (Button) findViewById(R.id.Alphabet);
}

Want to Run a function without click any button when application start

I have a function named 'func()'. I want to run this function when application start without clicking any button. just when application load I want to show a massage.that massage in that function. I just want to run that function when app start what will be the code.
public class TextViewActivity extends Activity {
public static EditText etxt;
public final void func(){
etxt.setText("Massage");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
}
}
Just put a call to the function on the onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func(); //A call to the function.
}
Hope that helps.
I don't recommend subclassing the Application in order to do this. When the application starts it will go to the main activity. So I would say just keep a SharedPreference boolean value if it has been set. If not show the message.
So keep state of the application here: http://developer.android.com/reference/android/content/SharedPreferences.html, just set a boolean. Remember when you app gets called, the activity onCreate always gets called of the main activity, so its just a matter of not calling it again.
try using below code.. you need to call ur function after you initialize edittext etxt. so it can not cause you NPE
public final void func(){
etxt.setText("Message");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func();// here your function call.
}
Try this:
public class TextViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.etxt2).setText("SMTH");
}
}
In case you really need a function to be called, you can use this:
public class TextViewActivity extends Activity {
public final void func() {
findViewById(R.id.etxt2).setText("SMTH");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
func();
}
}

Categories

Resources