Why must be objects initialized in onCreate? - android

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.

Related

setContentView in ActionBarActivity and FragmentActivity

In FragmentActivity, the order of super.onCreate and setContentView isn't important, why?
FragmentActivity
//OK
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_waiting_for_confirmation_order);
}
//OK
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_waiting_for_confirmation_order);
super.onCreate(savedInstanceState);
}
But in ActionBarActivity, it throws a NullPointerException.
ActionBarActivity
//OK
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_waiting_for_confirmation_order);
}
//ERROR
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_waiting_for_confirmation_order); //NullPointerException
super.onCreate(savedInstanceState);
}
The reason is ActionBarActivity (from support-v7) is using a delegate object to either use the real implementation or the compat implementation.
This delegate is instantiated in the method onCreate() of ActionBarActivity and the method setContentView() of ActionBarActivity is simply doing delegate.setContentView().
That's why there's a NPE if you call setContentView() before onCreate().
In FragmentActivity, (or standard Activity actually), the order doesn't matter because setContentView() doesn't rely on a specific object that could have been instantiated in onCreate().

What could possibly be causing this NPE in onCreate?

I am getting an NPE in onCreate of the following file (MySubActivity):
public class MySubActivity extends MySuperActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myTextView.setText(getResources().getString(R.string.myString));
}
}
MySuperActivity:
public class MySuperActivity extends Activity {
protected TextView myTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
myTextView = (TextView)findViewById(R.id.myTextViewid);
}
}
The strange thing is that I have never seen this crash while testing the app. The page works fine when I test it. However I got a crash report from Google notifying me of the crash. I cannot reproduce it, and I have no idea under what scenario this crash could happen. Seeing as how it works for me, the resource ids and string names etc. must be correct.
The only thing that came across my mind was that maybe the user had their phone set to a different language, so it couldn't properly pull the resources. However, there are default resources for all of them, and I tested changing the language of my emulator and it didn't crash. Any ideas?
Set your view in another method, like this:
public class MySubActivity extends MySuperActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void setView(){
myTextView.setText(getResources().getString(R.string.myString));
}
}
Edit: Don't call setView() in MySuperActivity
public class MySuperActivity extends Activity {
protected TextView myTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
myTextView = (TextView)findViewById(R.id.myTextViewid);
// Or, you can do this in a method called getView() if you like
}
}

How to set the titlebar in every activity programmatically?

Is it possible to set the titlebar in every activity programmatiaclly from one particular activity?
I can set the titlebar of a particular activity programmatically from within that activity, but subsequent activities have the string set from the manifest thereafter.
To set a title I use the following from within the activity. I would like every activity set like this in the application.
setTitle(carerName + " is now logged in");
you can write base activity and call a method of super .
public class DerivedActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...}
}
public class DerivedActivity2 extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...}
}
public class BaseActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(carerName + " is now logged in");
}
}

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();
}
}

How to call function of one activity from another activity

Actually i want to call a function of one activity from another activity..i write a simple code for that but its not working..and show runtime error..please check the code...if there any mistake..
code for activity1:
public class Activity1 extends Activity2
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
call();
}
public void call()
{
showToast("Helloo");
}
}
code for activity2:
public class Activity2 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showToast(String s)
{
EditText t=(EditText)findViewById(R.id.editText1);
t.setText(s);
}
}
Your problem is that you're calling findViewById on a view that doesn't exist.
Activity1 is extending Activity2.
You call the super.onCreate in Activity1 which calls the onCreate in Activity2 which calls setContentView() for R.layout.main.
I'm guessing your text R.id.editText1 is in the main layout.
When Activity1 returns from the call to super.onCreate it immediately resets the content layout to main2.
The edit text box that you are trying to edit no longer exists. findViewById can not find it because the layout is not active. Thus it crashes.
To fix it try this:
public class Activity1 extends Activity2
{
private EditText et;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
et = (EditText) findViewById(R.id.editText2);
call();
}
public void call()
{
showToast("Helloo", et);
}
}
Where R.id.editText2 is an edit text box in the layout main2.
In Activity2:
public class Activity2 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showToast(String s, EditText t)
{
t.setText(s);
}
}
First, this is a bad design principle since only one Activity is active at a time. You can make a method static and then you can cross call them but at that point it should be in some sort of common util class.
The simplest way is to declare your showToast() method as public static, this way you can call it without having an instance of Activity2.
if you put it in as static you should declare it on your main activity

Categories

Resources