在选项卡内动态添加文本视图(Android)

时间:2012-01-27 10:56:15

标签: android dynamic tabs textview add

我正在尝试动态地在选项卡中添加textview。使用此代码

Oncreate()
{  
    OA.loaderShow(this); //Loader display  
    new Thread(new Runnable(){  
        public void run()
        {  
            Looper.prepare();  
            fetchDocs();  
            OA.loaderHide(); //Loader Hide  
            Looper.loop();  
        }  
    }).start();  
}  

fetchDocs()
{  
    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);    
    TextView text = new TextView(this);              
    text.setText(mytext);        
    layout.addView(text);  
}

我收到此错误“只有创建视图层次结构的原始线程才能触及其视图”。

请帮助。

3 个答案:

答案 0 :(得分:1)

将上面的内容放在下面的块

runOnUiThread(new Runnable() {@Overridepublic void run() {//your code here}}

答案 1 :(得分:0)

在onCreate方法中添加它,而不是从else添加。

答案 2 :(得分:0)

尝试使用这样的处理程序:

编辑:

protected static final int SET_TEXTVIEW = 0;
Oncreate()
{  
    OA.loaderShow(this); //Loader display  
    new Thread(new Runnable(){  
        public void run()
        {  
            Looper.prepare();  
            handler.sendMessage(handler.obtainMessage(SET_TEXTVIEW)); 
            OA.loaderHide(); //Loader Hide  
            Looper.loop();  
        }  
    }).start();  
} 
public Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case SET_TEXTVIEW :
                    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
                    TextView text = new TextView(this);
                    text.setText(mytext);
                    layout.addView(text);
                }
            }
        };