Android 活动未正确更新
我是android新手,所以可能我做错了什么。我想有一个特定的活动,显示游戏中“生物”类实例的详细信息。名字,损失,诸如此类的事情 我在GUI对象中正确显示生物数据时遇到问题。无论是在初始创建时(它应该将该生物的名称复制到名称字段中),还是在添加伤害标记时(它不会更新以显示正确的图像) 以下是我的小例子:Android 活动未正确更新,android,android-layout,Android,Android Layout,我是android新手,所以可能我做错了什么。我想有一个特定的活动,显示游戏中“生物”类实例的详细信息。名字,损失,诸如此类的事情 我在GUI对象中正确显示生物数据时遇到问题。无论是在初始创建时(它应该将该生物的名称复制到名称字段中),还是在添加伤害标记时(它不会更新以显示正确的图像) 以下是我的小例子: public class CreatureDetailActivity2 extends Activity { Creature creature; public void addM
public class CreatureDetailActivity2 extends Activity
{
Creature creature;
public void addMark(View v)
{
// connected to the button via android:onClick="addMark" in the XML
creature.getTrack().addDamage(DamageType.Normal, 1);
refreshDisplay();
new AlertDialog.Builder(this).setTitle(creature.getName())
.setMessage(creature.getTrack().toString()).show();
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creature_detail);
creature = new Creature("Example");
refreshDisplay();
}
public void refreshDisplay()
{
final View creatureDetailView = this.getLayoutInflater().inflate(
R.layout.activity_creature_detail, null);
final EditText nameField = (EditText) (creatureDetailView
.findViewById(R.id.textbox_creature_name));
nameField.setText(creature.getName());
final ImageView damageBox0 = (ImageView) (creatureDetailView.findViewById(R.id.damageBox0));
damageBox0.setImageResource(R.drawable.__n);
// in the full program this does the same for 0 through 9, but this is a sample
// also, in the full program, this is a dynamic lookup for the correct pic
// but again, this is just a sample version.
}
}
现在的问题是,应用程序将加载并启动,但是没有一个小部件会正确更新。您可以单击按钮,它将显示AlertDialog,AlertDialog的文本将更改,但活动中的文本字段不会更改,并且ImageView从开始到应该更改的任何点都不会更改
所以我很困惑。如果我遗漏了一些重要的东西,我可以发布更多关于项目设置的信息,但我甚至不确定问题出在哪里,所以我不确定我的问题中还包括哪些内容
final View creatureDetailView = this.getLayoutInflater().inflate(
R.layout.activity_creature_detail, null);
将活动的布局基本上膨胀为零,只返回它膨胀的视图setContentView
实际上是将布局扩展到活动的视图层次结构中
一旦你膨胀你的布局,你不需要再这样做。只需使用findViewById
,而不引用悬空的独立视图
将刷新显示方法更改为:
public void refreshDisplay()
{
final EditText nameField = (EditText) findViewById(R.id.textbox_creature_name);
nameField.setText(creature.getName());
final ImageView damageBox0 = (ImageView) findViewById(R.id.damageBox0);
damageBox0.setImageResource(R.drawable.__n);
// in the full program this does the same for 0 through 9, but this is a sample
// also, in the full program, this is a dynamic lookup for the correct pic
// but again, this is just a sample version.
}
将活动的布局基本上膨胀为零,只返回它膨胀的视图setContentView
实际上是将布局扩展到活动的视图层次结构中
一旦你膨胀你的布局,你不需要再这样做。只需使用findViewById
,而不引用悬空的独立视图
将刷新显示方法更改为:
public void refreshDisplay()
{
final EditText nameField = (EditText) findViewById(R.id.textbox_creature_name);
nameField.setText(creature.getName());
final ImageView damageBox0 = (ImageView) findViewById(R.id.damageBox0);
damageBox0.setImageResource(R.drawable.__n);
// in the full program this does the same for 0 through 9, but this is a sample
// also, in the full program, this is a dynamic lookup for the correct pic
// but again, this is just a sample version.
}
什么都不会改变,因为你完全错了 如果要更新当前活动的任何视图元素,请按如下方式进行
View v = findViewById(R.id.element);
v.setText("text");
这只是一个简单的例子。
您需要将返回的元素强制转换为正确的类型,以便能够访问所有可用的方法
你做错的是试图再次膨胀布局。没有任何变化,因为你完全做错了 如果要更新当前活动的任何视图元素,请按如下方式进行
View v = findViewById(R.id.element);
v.setText("text");
这只是一个简单的例子。
您需要将返回的元素强制转换为正确的类型,以便能够访问所有可用的方法
您所做的错误是尝试再次膨胀布局