添加选项卡时获取nullpointer异常?

时间:2014-02-10 07:24:51

标签: android tabs

您好我正在尝试添加标签我正在获得nullpointer异常。我正在创建一个活动MyLanguageActivity这扩展了Activity我又创建了一个活动MyTabActivity这扩展了TabActivity.Inside of MyLangugeActivity我选择语言而且我正在点击完成按钮我再次添加标签这样:

public void setTabs(){
Intent intent, intent1, intent2, intent3;
    TabHost tabHost = MyTabActivity.tabHost;
    System.out.println(tabHost);
    TextView tv;
    intent = new Intent(
            "com.activitygroups.RequestTabActivtyGroup");
    intent1 = new Intent(
            "com.activitygroups.OffersTabActivityGroup");

    intent.putExtra("content", "requests");
    intent1.putExtra("content", "offers");


    System.out.println("request string : "
            + getResources().getString(R.string.requests));
    System.out.println("drowable id : "
            + getResources().getDrawable(R.drawable.request_tab_selector));

    tabHost.addTab(tabHost
            .newTabSpec("request_tab")
            .setIndicator(
                    LanguageUtils.getLanguage(MyLanguageActivity.this,
                            getResources().getString(R.string.requests)),
                    getResources().getDrawable(
                            R.drawable.request_tab_selector))
            .setContent(
                    new Intent(intent)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

}

里面的og done按钮onclick我正在调用这个方法。当我点击完成后我得到nullpointer异常..我的logcat在这里:

02-10 12:32:37.112: E/AndroidRuntime(5560): FATAL EXCEPTION: main
02-10 12:32:37.112: E/AndroidRuntime(5560): java.lang.NullPointerException
02-10 12:32:37.112: E/AndroidRuntime(5560):     at         android.widget.TabWidget.setCurrentTab(TabWidget.java:339)
02-10 12:32:37.112: E/AndroidRuntime(5560):     at android.widget.TabWidget.focusCurrentTab(TabWidget.java:363)
02-10 12:32:37.112: E/AndroidRuntime(5560):     at android.widget.TabHost.setCurrentTab(TabHost.java:320)
02-10 12:32:37.112: E/AndroidRuntime(5560):     at android.widget.TabHost.addTab(TabHost.java:213)
02-10 12:32:37.112: E/AndroidRuntime(5560):     at com.melhoroferta.LanguageSettingActivity.setUpTabs(MyLanguageActivity.java:204)
02-10 12:32:37.112: E/AndroidRuntime(5560):     at com.melhoroferta.LanguageSettingActivity.onClick(MyLanguageActivity.java:142)
02-10 12:32:37.112: E/AndroidRuntime(5560):     at android.view.View.performClick(View.java:2408)
02-10 12:32:37.112: E/AndroidRuntime(5560):     at android.view.View$PerformClick.run(View.java:8818)
02-10 12:32:37.112: E/AndroidRuntime(5560):     at   android.os.Handler.handleCallback(Handler.java:587)
  02-10 12:32:37.112: E/AndroidRuntime(5560):   at android.os.Handler.dispatchMessage(Handler.java:92)
        02-10 12:32:37.112: E/AndroidRuntime(5560):     at android.os.Looper.loop(Looper.java:123)
         02-10 12:32:37.112: E/AndroidRuntime(5560):    at android.app.ActivityThread.main(ActivityThread.java:4627)
          02-10 12:32:37.112: E/AndroidRuntime(5560):   at java.lang.reflect.Method.invokeNative(Native Method)
  02-10 12:32:37.112: E/AndroidRuntime(5560):   at java.lang.reflect.Method.invoke(Method.java:521)
   02-10 12:32:37.112: E/AndroidRuntime(5560):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
   02-10 12:32:37.112: E/AndroidRuntime(5560):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
        02-10 12:32:37.112: E/AndroidRuntime(5560):     at dalvik.system.NativeStart.main(Native Method)

对不起家伙我忘记删除那些意图2和意图3实际上这个意图是我不使用..

2 个答案:

答案 0 :(得分:4)

 intent = new Intent(
            "com.activitygroups.RequestTabActivtyGroup");
 intent1 = new Intent(
            "com.activitygroups.OffersTabActivityGroup");

添加Intents

 intent2 = new Intent(
            "profile activity");
 intent3 = new Intent(
            "setting activity");

答案 1 :(得分:0)

您必须初始化intent2intent3。因为您使用.putExtra来2未初始化意图

只需添加:

intent2 = new Intent(); //put in the brackets what you want or need
intent3 = new Intent();