2013-05-03 77 views
1

我是新的android程序员,我不知道如何在OnTabChangeListener调用之后设置TextView的新文本。我尝试了很多方法,但每次都显示奇怪的错误。如何通过tabhost监听器更改textview

编辑:(最新的代码)

MainActivity.java:

package com.RobsoN; 

import android.app.Activity; 
import android.app.TabActivity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TabHost; 
import android.widget.TextView; 
import android.widget.TabHost.OnTabChangeListener; 
import android.widget.TabHost.TabSpec; 

public class MainActivity extends TabActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final TabHost tabHost = getTabHost(); 

    Log.i("App","App initialization"); 

    TabSpec start = tabHost.newTabSpec("Start"); 
    start.setIndicator("Start", getResources().getDrawable(android.R.drawable.ic_menu_rotate)); 
    Intent photosIntent = new Intent(this, Start.class); 
    start.setContent(photosIntent); 

    TabSpec settings = tabHost.newTabSpec("Ustawienia"); 
    settings.setIndicator("Ustawienia", getResources().getDrawable(android.R.drawable.ic_menu_manage)); 
    Intent songsIntent = new Intent(this, Settings.class); 
    settings.setContent(songsIntent); 

    TabSpec info = tabHost.newTabSpec("Informacje"); 
    info.setIndicator("Informacje", getResources().getDrawable(android.R.drawable.ic_menu_help)); 
    Intent videosIntent = new Intent(this, Other.class); 
    info.setContent(videosIntent); 

    // Adding all TabSpec to TabHost 
    tabHost.addTab(start); 
    tabHost.addTab(settings); 
    tabHost.addTab(info); 

    tabHost.setOnTabChangedListener(new OnTabChangeListener(){ 

     public void onTabChanged(String tabId) { 
      if(tabId.equals("Informacje")) 
      { 
       Other child = (Other) getTabHost().getChildAt(0).getContext(); //Error here: 1 java.lang.ClassCastException: com.RobsoN.MainActivity 2 com.RobsoN.MainActivity$1.onTabChanged(MainActivity.java:50) 

       child.refreshInfo();  
      }  
     }}); 

} 

}

Other.java:

package com.RobsoN; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class Other extends Activity { 

Button button_update; 
TextView stat_lastestappversion; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tab_other); 

    stat_lastestappversion = (TextView) findViewById(R.id.stat_lastestappversion); 
    button_update = (Button) findViewById(R.id.button_update); 
    button_update.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 

     } 
    }); 
} 

public void refreshInfo() 
{ 
    stat_lastestappversion = (TextView) findViewById(R.id.stat_lastestappversion); 
    stat_lastestappversion.setText("TEST TEST 321"); 
} 
} 

错误:

05-05 13:00:59.845: E/AndroidRuntime(314): FATAL EXCEPTION: main 
05-05 13:00:59.845: E/AndroidRuntime(314): java.lang.ClassCastException: com.RobsoN.MainActivity 
05-05 13:00:59.845: E/AndroidRuntime(314): at com.RobsoN.MainActivity$1.onTabChanged(MainActivity.java:50) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:356) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.widget.TabHost.setCurrentTab(TabHost.java:341) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.view.View.performClick(View.java:2408) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.view.View$PerformClick.run(View.java:8816) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.os.Handler.handleCallback(Handler.java:587) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.os.Looper.loop(Looper.java:123) 
05-05 13:00:59.845: E/AndroidRuntime(314): at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-05 13:00:59.845: E/AndroidRuntime(314): at java.lang.reflect.Method.invokeNative(Native Method) 
05-05 13:00:59.845: E/AndroidRuntime(314): at java.lang.reflect.Method.invoke(Method.java:521) 
05-05 13:00:59.845: E/AndroidRuntime(314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-05 13:00:59.845: E/AndroidRuntime(314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-05 13:00:59.845: E/AndroidRuntime(314): at dalvik.system.NativeStart.main(Native Method) 
+0

你不能简单的实例化一个活动。使用片段框架来制作标签。 – Luksprog 2013-05-03 21:19:50

回答

0

作为Luksprog说,你不能像在java中那样直接使用活动的初始化。据我所见,你想在你的标签主机的子活动中显示一个textview。要做到这一点,你可以使用下面的

Other child = (Other) getTabHost().getChildAt(0).getContext(); 
    child.refreshInfo(); 

注:

由于您是新的android程序员,强烈建议您以Fragments开头。标签概念很久以前就被剥夺了。旧的程序员可能仍然在为支持他们现有的程序而努力,但作为一名新程序员,你最好避免它。

UPDATE:

你可以做的另一件事

getTabHost().setCurrentTab(tabindex); 
Other child= (Other) this.getCurrentActivity(); 
child.refreshInfo(); 
+1

谢谢您提供完整且可以理解的答案。我是一名年轻的程序员,我不太懂英语,无法理解Android手册,但我会继续尝试。 – user2321517 2013-05-04 09:50:20

+0

欢迎您:) – stinepike 2013-05-04 09:54:57

+0

对不起,我没有检查发布前的代码,我试图通过不同的方式来解决它自我,但我总是得到错误:'java.lang.ClassCastException:com.RobsoN.MainActivity \t at com.RobsoN.MainActivity $ 1.onTabChanged(MainActivity.java:70) \t在android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:356) \t在android.widget.TabHost.setCurrentTab(TabHost.java:341)' – user2321517 2013-05-04 10:32:48