简体   繁体   English

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

[英]why does gradientdrawable.setBackground makes my app crash?

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; 
    }

Theres no compilation error, it just crashes when I test drive it 没有编译错误,当我测试驱动器时它只会崩溃

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

Please change above line as 请更改以上行为

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

your linear layout remain null that's why it crashing 您的线性布局保持为空,这就是它崩溃的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM