@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);  
        LinearLayout linear=(LinearLayout)inflater.inflate(R.layout.main_xml,null);
        tabhost=(TabHost)linear.findViewById(R.id.detail_goods_tab);
        tabhost.setup(this.getLocalActivityManager());
        getTabHost();
        main_tab_container.removeAllViews();
main_tab_container.addView(linear);
        
}
其中 tabhost.setup(this.getLocalActivityManager());这一行报空指针异常。请问一下这是什么回事?急,谢谢哪位了,我找了查了一天都没有查到结果,

解决方案 »

  1.   

    你的这个Activity继承的是TabActivity还是Activity?继承TabActivity就直接使用TabHost tab = getTabHost();还有,把代码贴全了吧,另外吧LOG也贴一下,要不然不好找的
      

  2.   

    public class MainActivity extends BaseActivity {
       
    TabHost tabhost ; 
    TabWidget tabWidget;
    TextView all_evaluate, good_evaluate,mid_evaluate,bad_evaluate;

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);  
            LinearLayout linear=(LinearLayout)inflater.inflate(R.layout.main_xml,null);
     
            tabhost=(TabHost)linear.findViewById(R.id.detail_goods_tab);
            tabhost.setup(this.getLocalActivityManager());
            getTabHost();
            main_tab_container.removeAllViews();
    main_tab_container.addView(linear);
            
    }
       
        public void getTabHost(){
         tabhost.addTab(tabhost.newTabSpec("introduce")
             .setIndicator("全部评论")
             .setContent(new Intent(this,All_Evaluate_View.class)));
         tabhost.addTab(tabhost.newTabSpec("specification")
             .setIndicator("好评1344")
             .setContent(new Intent(this,Good_Evaluate_View.class)));
         tabhost.addTab(tabhost.newTabSpec("pacagelist")
             .setIndicator("中评101")
             .setContent(new Intent(this,Mid_Evaluate_View.class)));
         tabhost.addTab(tabhost.newTabSpec("aftersale")
             .setIndicator("差评30")
             .setContent(new Intent(this,Bad_Evaluate_View.class)));
         tabhost.setCurrentTab(0);
    tabWidget=tabhost.getTabWidget();
    set_tabWidget();
        }
        
        public void set_tabWidget(){ int height=50;
    int width=getWindowManager().getDefaultDisplay().getWidth()/4;
     for (int i =0; i < tabWidget.getChildCount(); i++) 
       {   
        /**  
        * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果  
        */  
        tabWidget.getChildAt(i).getLayoutParams().height = height;   
        tabWidget.getChildAt(i).getLayoutParams().width = width;         
           /**  
        * 设置tab中默认标题文字的颜色,不然默认为黑色  
       */  
        if(i==0){
       TextView tv1 = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);  
       /*SpannableStringBuilder style=new SpannableStringBuilder(tv1.getText());   
    style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
    tv1.setText(style);*/
        tv1.setTextColor(Color.BLACK);
        tv1.setTextSize(12);
        tv1.setPadding(15, 5, 15, 8);
        }else{
         TextView tv1 = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title); 
         int tv_leng=tv1.getText().toString().length();
       SpannableStringBuilder style=new SpannableStringBuilder(tv1.getText());   
    style.setSpan(new ForegroundColorSpan(Color.RED),2,tv_leng,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
    tv1.setText(style);
    tv1.setTextColor(Color.BLACK);
    tv1.setTextSize(12);
        tv1.setPadding(15, 5, 15, 8);
        }
       View vvv = tabWidget.getChildAt(i);  
        //如果是当前选中的tab 设置其背景颜色
       if(tabhost.getCurrentTab()==i){   
       vvv.setBackgroundResource(R.drawable.tab_state1);
         }   
       else {   
     vvv.setBackgroundResource(R.drawable.tab_state2);
         }   
                        
         }
    这是我代码,请lingang1359你帮我看看吧,谢谢了
      

  3.   

    BaseActivity 这个类是我的程序的一个基本类,它是继承activity类的