更改了 main.xml 中的顺序,现在我得到 ClassCastException
使用 eclipse 和 Android SDK,我管理了一个带有 Button 和 ProgressBar 的简单测试应用程序。一切运行正常,除了当 ProgressBar 可见时我不希望 ProgressBar 移动按钮,因此为了测试,我更改了它们在 res/layout/main.xml 文件中定义的顺序(该文件使用线性布局)。编译并运行,然后我在下面的“final ProgressBar ...”行得到一个 ClassCastException。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 01-06 14:37:39.590: E/AndroidRuntime(863): java.lang.RuntimeException:
java.lang.ClassCastException: android.widget.Button cannot be cast to
android.widget.ProgressBar */
final ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressBar1); /* here */
progressbar.setVisibility(ProgressBar.GONE);
final Button exebutton = (Button)findViewById(R.id.button1);
exebutton.setOnClickListener(new View.OnClickListener()
// etc...
现在,我明白了 ClasCastException 的含义和含义,但我只是不明白它为什么会出现。我不想将按钮投射到进度条上。我不明白...
Using eclipse and the Android SDK, I managed a simple test app with a Button and a ProgressBar. All runs fine, except I did't want the ProgressBar to move the Button, when the ProgressBar was made visible, so just for testing I changed the order that they are defined in the res/layout/main.xml file (which uses a LinearLayout). Compiling and running I then get a ClassCastException at the "final ProgressBar ..." line below.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 01-06 14:37:39.590: E/AndroidRuntime(863): java.lang.RuntimeException:
java.lang.ClassCastException: android.widget.Button cannot be cast to
android.widget.ProgressBar */
final ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressBar1); /* here */
progressbar.setVisibility(ProgressBar.GONE);
final Button exebutton = (Button)findViewById(R.id.button1);
exebutton.setOnClickListener(new View.OnClickListener()
// etc...
Now, I understand what the ClasCastException says and means, I just don't understand why it appears. I am not trying to cast a Button to a ProgressBar. I don't get it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试清理项目,以便再次生成 R 类。有时这些值不会更新。
Try cleaning the project so the R class gets generated again. Sometimes the values dont update.
它看起来像这一行:
正在将按钮投射到进度条。
这意味着
findViewById
返回R.id.progressBar1
的按钮。既然你说你改变了订单,看起来这个 id 仍然对应于按钮。这表明生成的文件有问题。 我会做一个项目/清理。
It looks like this line:
is casting a Button to a progressbar.
This means that the
findViewById
returns the button forR.id.progressBar1
.Since you are saying you changed the order, this looks this id still corresponds to the button. This points to a problem with the generated file. I would do a Project/Clean.