gpt4 book ai didi

android - 申请意外中断

转载 作者:行者123 更新时间:2023-12-03 17:41:13 25 4
gpt4 key购买 nike

为什么,在这个非常简单的例子中,最后一条语句 (setBackgroundColor)
导致应用程序崩溃?

public class Macumba extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView vista = (ImageView)findViewById(R.id.vista);
vista.setBackgroundColor(Color.YELLOW);
}
}

main.xml 也很简单:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="@+id/vista"
android:layout_width="300px"
android:layout_height="330px"
android:layout_x="9px"
android:layout_y="8px"
/>
</AbsoluteLayout>

最佳答案

你得到什么样的异常(exception)?空指针异常?如果是这样,很可能是因为 vista 为空,这是因为 R.id.vista不在您的布局中。

你确定它是正确的 main.xml 文件吗?而不是来自例如。另一个项目?

关于android - 申请意外中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9274209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com