- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是新的android程序员,我不知道如何在OnTabChangeListener调用后设置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();
}
}});
}
}
其他.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)
最佳答案
正如 Luksprog 所说,您不能像在 java 中那样使用 Activity 的直接初始化。据我所知,您想在选项卡主机的子 Activity 中显示 TextView 。为此,您可以使用以下内容
Other child = (Other) getTabHost().getChildAt(0).getContext();
child.refreshInfo();
注意
由于您是新的 Android 程序员,强烈建议从 Fragments 开始。选项卡概念很久以前就已被弃用。老程序员可能仍然会使用它来支持他们现有的程序,但作为新程序员,你最好避免它。
更新:
你可以做另一件事
getTabHost().setCurrentTab(tabindex);
Other child= (Other) this.getCurrentActivity();
child.refreshInfo();
关于java - 如何通过 tabhost 监听器更改 textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16366578/
当单击复选框(或选择所有复选框)时,我想向 CheckboxSelectionModel 添加一个监听器。 var smSensors = new xg.CheckboxSelectionModel(
我有一个简单的程序,允许 2 个客户端连接到服务器。 连接后,他们可以轮流点击空白卡片图像。 一旦 2 个客户中的任何一个点击空白卡片图片,卡片图片将变为 Ace 俱乐部图片。 更改将显示在客户端的两
我在这里看到了一个代码,该代码以字符串的形式检索鼠标的当前图标,但是此代码使用了TTimer来实现。 因此,我想知道是否存在某些事件(侦听器)来检测鼠标光标图标上的这些更改。 下面是使用TTimer的
我想在我的配置对象上获得一个 onload 事件。 以下工作,除非我创建一个 config.listeners={..} (我认为这就是我需要的?)替换 this.onload({...}); 我什至
通常,在 Java 中,当我有一个向其他对象提供某种通知的对象时,我将使用 Listener/Observer 模式。 有没有更类似于 Scala 的方式来做到这一点?我应该在 Scala 中使用这种
我有一个带有动画器的游戏对象和一些可以触发事件的动画(具有特定的回调函数)。 只要我将脚本添加到与动画器相同的游戏对象(包括接收器),一切都会正常工作: public class AnimatorEv
我有一个带有监听器的 DialogFragment,用于单击按钮以调用 fragment 中的函数。 我收到 lateinit property listener has not been initi
这个问题已经有答案了: Java ActionListener error: incompatible types (4 个回答) 已关闭 5 年前。 我最近刚刚开始学习 Java 代码。我在添加监听
我的代码遇到问题。我想知道是否有一种更简单的方法来使用监听器,而不是不断地这样做: example.addActionListener(new java.awt.event.ActionListene
有没有办法使用 .net 创建控制台应用程序。或通过某个端口监听 SMTP 消息的服务? 我需要创建一个中间层对象来捕获和处理 smtp 消息。也就是说,我希望该监听器发送和接收 smtp 消息,然后
我有一个带有动画器的游戏对象和一些可以触发事件的动画(具有特定的回调函数)。 只要我将脚本添加到与动画器相同的游戏对象(包括接收器),一切都会正常工作: public class AnimatorEv
我有许多向主事件生成服务注册的监听器。然而,我想告诉听众,事件流在某个时刻将会结束。您会通过简单地调用监听器上的方法(例如 finish())来完成此操作,还是有一个单独的事件方法 streamFin
我的代码有什么问题。 我创建了一个 JList,添加了项目并将其推到左侧(BorderLayout.WEST)。每次单击列表项时,我希望在列表右侧显示一个面板。但问题是,当选择列表项并运行监听器时,到
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
这可能是一个简单的问题,但我没有看到它。 我有一个界面 public interface RenderableListener{ public void update(T element);
有人可以直接指出我的正确方向吗?当从组合框中选择适当的选项时,我希望小程序中的 Action 监听器显示从 html 文件检索的 jlabel 中的 3 个参数之一。 干杯 最佳答案 对于组合框,您需
我有一个网站,每个页面上都有许多 jQuery 事件处理程序,所有这些都在一个大型 .js 文件中。 这意味着对于任何页面,大多数事件处理程序都是针对不存在且根本不会使用的 HTML。 这会影响我的表
我有一些 jQuery 监听器设置,用于监听 type="text" 字段上的表单输入。但是,当用户从自动完成下拉框中选择一个选项(即他们之前输入的值已被记住以供将来使用)时,下面的监听器不会收集该值
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我正在用 unity (c#) 做一个类似国际象棋的游戏,但我在尝试进行向上转换以将信息从一个 child 发送到另一个抽象类时遇到了困难。 基本上,我有一个抽象类,它有一个带有函数的事件/委托(de
我是一名优秀的程序员,十分优秀!