javaandroidandroid-layoutgradientdrawable

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


Solution

  • 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