gpt4 book ai didi

java - 为什么gradientdrawable.setBackground会让我的应用程序崩溃?

转载 作者:太空宇宙 更新时间:2023-11-04 11:00:14 25 4
gpt4 key购买 nike

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(3, Color.GREEN);

View view = inflater.inflate(R.layout.primary, container, false);
LinearLayout layout = (LinearLayout ) view.findViewById(R.layout.primary);
// 1 layout.setBackground(fragmentBorder); <----
// ^ above line causes the crash ^

layout.setBackgroundDrawable(drawable);
return view;
}

没有编译错误,只是在测试时崩溃了

最佳答案

LinearLayout layout = (LinearLayout ) view.findViewById(R.layout.primary);

请将上面的行更改为

 LinearLayout layout = (LinearLayout ) view.findViewById(R.id.primary);

你的线性布局仍然为空,这就是它崩溃的原因

关于java - 为什么gradientdrawable.setBackground会让我的应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46986440/

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