NullpointerException when opening a tabhost activity

I am working on an application where i moving from list menu to tabhost activity ,that is

Menu.class -> Recharge.class where Recharge.class is a TabActivity having a tab named prepaid.class

I need to pass a string from menu.class to prepaid.class in the below code i passed the string please find the bug in my code and help me

Menu.class

    if(s.equals("Recharge"))
        {
            Intent i = new Intent(menu.this,recharge.class);
            i.putExtra("app",app);
            startActivity(i);

        }

Recharge.class

Intent i = this.getIntent();            
    final String app=(String)i.getStringExtra("app");
   Resources res = getResources(); 
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec; 
    Intent intent;

    intent = new Intent().setClass(this, prepaid.class);
    intent.putExtra("app", app);

    //Prepaid
    spec = tabHost.newTabSpec("PREPAID").setIndicator("",
                      res.getDrawable(R.drawable.prepaid))
                  .setContent(intent);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabhost);

prepaid.class

Intent i=this.getIntent();
final String app=(String)i.getStringExtra("app");

This is the error in log:

09-10 14:37:44.038: E/AndroidRuntime(1172): FATAL EXCEPTION: main
09-10 14:37:44.038: E/AndroidRuntime(1172): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ezerecharge/com.example.ezerecharge.recharge}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ezerecharge/com.example.ezerecharge.prepaid}: java.lang.NullPointerException
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.os.Looper.loop(Looper.java:123)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invoke(Method.java:507)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at dalvik.system.NativeStart.main(Native Method)
09-10 14:37:44.038: E/AndroidRuntime(1172): Caused by: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ezerecharge/com.example.ezerecharge.prepaid}: java.lang.NullPointerException
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.widget.TabHost.addTab(TabHost.java:216)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at com.example.ezerecharge.recharge.onCreate(recharge.java:33)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-10 14:37:44.038: E/AndroidRuntime(1172):     ... 11 more
09-10 14:37:44.038: E/AndroidRuntime(1172): Caused by: java.lang.NullPointerException
09-10 14:37:44.038: E/AndroidRuntime(1172):     at com.example.ezerecharge.prepaid.<init>(prepaid.java:22)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at java.lang.Class.newInstanceImpl(Native Method)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at java.lang.Class.newInstance(Class.java:1409)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)

Answers


You are calling getIntent in the constructor. You cannot use it before the onCreate is called.

Move your intitialization code from prepaid in prepaid.onCreate.

And start your classes names by capital letters. please.


Need Your Help

boost::asio::async_write not writing to clients properly, weird behaviour

boost boost-asio

I have written a program that accepts N client connections and then writes data into them. The problem I am having now is: I can only write to N-1 clients, the first one is never written to. I have...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.