1

Hi Friends i have written an Activity like below.

SplashScreen.java

public class SplashScreen extends Activity {

protected final int _splashTime = 4000;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    new Handler().postDelayed(new Runnable() {      
        public void run() {
            startActivity(mainIntent);*/
            Main mainObj=new Main();
            final Bundle bundle=new Bundle();
            mainObj.onCreate(bundle);
            finish();
        }
    }, _splashTime);
   }
}

From the above Activity iam calling Main.java which is as follows.

public class Main extends Activity{

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);//line 1.

        setContentView(R.layout.main1);

      }

}

In the above Main.java in the line 1 while calling super.onCreate(savedInstanceState) iam getting NullPointerException.I have identified that if i call the Activity by creating an object using new operator it is giving NullPointerException like below

Main mainObj=new Main();

final Bundle bundle=new Bundle();

mainObj.onCreate(bundle);

But if i call an Activity using

startActivity(new Intent(SplashScreen.this,Main.claass));

iam not getting NullPointerException.So how can i run an Activity by creating an object using new opertor with out NullPointerException.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
user1195614
  • 385
  • 2
  • 8
  • 20

3 Answers3

1

So how can i run an Activity by creating an object using new opertor with out NullPointerException.

You should never attempt to create an instance of an Activity using new. This is not how Android works. The Activity class is a special case in Android (along with several other special Android components) and you need to create and manage them correctly.

To create a new Activity you must always create a new Intent and use startActivity(...) or one of the other startActivityXXX(...) methods.

See Application Fundamentals

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Thank you for the response MisterSquonk. – user1195614 Mar 03 '12 at 00:57
  • @user1195614: You're welcome. Most of the classes used in Android can be treated the same way as any Java class but the core application components (such as `Activity`) need to be treated differently. Make sure to bookmark that link - it is very useful. – Squonk Mar 03 '12 at 01:09
0

Where is mainIntent defined? Try this in the thread:

public void run() {
        Intent mainIntent = new Intent(this, Main.class);
        startActivity(mainIntent);
        finish();
    }
Stuart Lacy
  • 1,963
  • 2
  • 18
  • 30
0

You will want to use the startActivity() method that you already seem to be able to get working. You do not want to directly call the onCreate() method of your sub-activity. There is a lot going on behind the scenes that Android is managing for you when you call startActivity.

Here is a link to the developer guide: Starting An Activity

Mark D
  • 3,317
  • 1
  • 26
  • 27