0

我正在开发一个应用程序,我从列表菜单移动到 tabhost 活动,即

Menu.class -> Recharge.class 其中 Recharge.class 是一个 TabActivity 有一个名为 prepaid.class 的选项卡

我需要在下面的代码中将一个字符串从 menu.class 传递给 prepaid.class 我传递了字符串请在我的代码中找到错误并帮助我

菜单类

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

        }

充值班

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

预付费类

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

这是日志中的错误:

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)
4

1 回答 1

1

您在构造函数中调用 getIntent 。在调用 onCreate 之前不能使用它。

将您的初始化代码从 prepaid.onCreate 中的 prepaid 移动。

并以大写字母开头您的班级名称。请。

于 2012-09-10T09:45:18.583 回答