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

时间:2017-10-28 05:06:09

标签: java android android-layout gradientdrawable

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没有编译错误,它只是在我试驾时崩溃

1 个答案:

答案 0 :(得分:0)

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

请将以上行改为

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

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