Android的 - 当添加/删除选项卡歌厅错误 [英] Android - when Adding/Deleting the Tab geting Error

查看:150
本文介绍了Android的 - 当添加/删除选项卡歌厅错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个code就像当我喜欢上的添加按钮,将添加标签和并没有删除按钮来删除它tab..But做工精细3〜4次,但后它抛出的错误...

和日志 - 猫是这样的: -

  02-28 16:33:37.832:E / AndroidRuntime(7830):致命异常:主要
02-28 16:33:37.832:E / AndroidRuntime(7830):显示java.lang.NullPointerException
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.widget.TabWidget.focusCurrentTab(TabWidget.java:461)
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.widget.TabHost.setCurrentTab(TabHost.java:410)
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.widget.TabHost $ 2.onTabSelectionChanged(TabHost.java:154)
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.widget.TabWidget $ TabClickListener.onClick(TabWidget.java:546)
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.view.View.performClick(View.java:4223)
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.view.View $ PerformClick.run(View.java:17487)
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.os.Handler.handleCallback(Handler.java:800)
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.os.Handler.dispatchMessage(Handler.java:100)
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.os.Looper.loop(Looper.java:194)
02-28 16:33:37.832:E / AndroidRuntime(7830):在android.app.ActivityThread.main(ActivityThread.java:5371)
02-28 16:33:37.832:E / AndroidRuntime(7830):在java.lang.reflect.Method.invokeNative(本机方法)
02-28 16:33:37.832:E / AndroidRuntime(7830):在java.lang.reflect.Method.invoke(Method.java:525)
02-28 16:33:37.832:E / AndroidRuntime(7830):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)
02-28 16:33:37.832:E / AndroidRuntime(7830):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-28 16:33:37.832:E / AndroidRuntime(7830):在dalvik.system.NativeStart.main(本机方法)

MainActivity.java

 包com.example.sampletab;进口android.app.TabActivity;
进口android.content.Intent;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TabHost;
进口android.widget.TabHost.OnTabChangeListener;
进口android.widget.TabHost.TabSpec;@燮pressWarnings(德precation)
公共类MainActivity扩展TabActivity {
    私有静态诠释的tabIndex = 0;
    私人TabHost tabHost;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        tabHost = getTabHost();        tabHost.setOnTabChangedListener(新OnTabChangeListener(){            @覆盖
            公共无效onTabChanged(字符串tabId){                //设置第一个选项卡为选定的选项卡。
                setSelectedTabColor();
                // setTabColor(tabHost);
            }
        });
        // setTabColor(tabHost);        addTabs();
        delTabs();        ((按钮)findViewById(R.id.add_tab))
                .setOnClickListener(新View.OnClickListener(){                    @覆盖
                    公共无效的onClick(视图v){
                        // TODO自动生成方法存根
                        的tabIndex ++;
                        addTabs();
                    }
                });
        ((按钮)findViewById(R.id.del_tab))
                .setOnClickListener(新View.OnClickListener(){                    @覆盖
                    公共无效的onClick(视图v){
                        // TODO自动生成方法存根
                        delTabs();
                    }
                });
    }    保护无效setSelectedTabColor(){
        // TODO自动生成方法存根
        的for(int i = 0; I< tabHost.getTabWidget()getChildCount();我++){
            tabHost.getTabWidget()。getChildAt㈠
                    .setBackgroundColor(Color.LTGRAY);
        }
        tabHost.getTabWidget()。getChildAt(tabHost.getCurrentTab())
                .setBackgroundColor(Color.RED);
    }    私人无效delTabs(){
        tabHost.getTabWidget()。removeView(
                tabHost.getTabWidget()。getChildTabViewAt(
                        tabHost.getCurrentTab()));    }
    私人无效addTabs(){
        // TODO自动生成方法存根
        LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
        按钮tabBtn =(按钮)layoutInflater.inflate(R.layout.tab_event,
                空值);
        tabBtn.setText(标签+的tabIndex);
        意图tabintent =新意图(MainActivity.this,BlankActivity.class);
        setupTab(tabBtn,tabintent,标签+的tabIndex);
    }    私人无效setupTab(查看tabBtn,意图setIntent,字符串变量){
        // TODO自动生成方法存根
        则tabspec setContent = tabHost.newTabSpec(标签).setIndicator(tabBtn)
                .setContent(setIntent);
        tabHost.addTab(setContent);
    }
}


解决方案

您需要在您添加标签到TabHost设置分隔绘制。

因此​​,以下行:

  tabHost.getTabWidget()setDividerDrawable(R.color.light)。

应向上移动。你可以以后如果直接调用:

  TabHost tabHost = getTabHost();

I'm trying to develop a code like when I like on the Add button it will Add the Tab and and there is Del button to Delete the tab..But It work fine for 3 to 4 time but after that it throws the error...

and Log-Cat look Like this:-

02-28 16:33:37.832: E/AndroidRuntime(7830): FATAL EXCEPTION: main
02-28 16:33:37.832: E/AndroidRuntime(7830): java.lang.NullPointerException
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.widget.TabWidget.focusCurrentTab(TabWidget.java:461)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.widget.TabHost.setCurrentTab(TabHost.java:410)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.view.View.performClick(View.java:4223)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.view.View$PerformClick.run(View.java:17487)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.os.Handler.handleCallback(Handler.java:800)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.os.Handler.dispatchMessage(Handler.java:100)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.os.Looper.loop(Looper.java:194)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at android.app.ActivityThread.main(ActivityThread.java:5371)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at java.lang.reflect.Method.invoke(Method.java:525)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-28 16:33:37.832: E/AndroidRuntime(7830):     at dalvik.system.NativeStart.main(Native Method)

and

MainActivity.java

package com.example.sampletab;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
    private static int tabIndex = 0;
    private TabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost = getTabHost();

        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {

                // set the First Tab as selected Tab.
                setSelectedTabColor();
                // setTabColor(tabHost);
            }
        });
        // setTabColor(tabHost);

        addTabs();
        delTabs();

        ((Button) findViewById(R.id.add_tab))
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        tabIndex++;
                        addTabs();
                    }
                });
        ((Button) findViewById(R.id.del_tab))
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        delTabs();
                    }
                });
    }

    protected void setSelectedTabColor() {
        // TODO Auto-generated method stub
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundColor(Color.LTGRAY);
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                .setBackgroundColor(Color.RED);
    }

    private void delTabs() {
        tabHost.getTabWidget().removeView(
                tabHost.getTabWidget().getChildTabViewAt(
                        tabHost.getCurrentTab()));

    }
    private void addTabs() {
        // TODO Auto-generated method stub
        LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
        Button tabBtn = (Button) layoutInflater.inflate(R.layout.tab_event,
                null);
        tabBtn.setText("Tabs" + tabIndex);
        Intent tabintent = new Intent(MainActivity.this, BlankActivity.class);
        setupTab(tabBtn, tabintent, "Tab" + tabIndex);
    }

    private void setupTab(View tabBtn, Intent setIntent, String tag) {
        // TODO Auto-generated method stub
        TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn)
                .setContent(setIntent);
        tabHost.addTab(setContent);
    }
}

解决方案

You need to set the divider drawable BEFORE you add tabs to the TabHost.

So the following line:

tabHost.getTabWidget().setDividerDrawable(R.color.light);

should be moved up. You can call if directly after:

TabHost tabHost = getTabHost();

这篇关于Android的 - 当添加/删除选项卡歌厅错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆