0

I've been working on making my own custom ImageView class on Android. I've read the documentation on layouts, fragments, activities and ImageView.

I've looked at Stack posts on how to create an ImageView programatically but I've run into a snag I can't seem find a solution to (reading the top hits for searching the above error did not apply to my issue).

I have an activity, and inside that activity I have a fragment. Inside the fragment I am creating an ImageView (its called iImage but its just an extended ImageView class).

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        iImage image = new iImage(getActivity());
        image.setImageResource(R.drawable.name_splash);
        LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(1500,1100);
        image.setLayoutParams(par);
        container.addView(image);
        View rootView = inflater.inflate(R.layout.fragment_edit_photo, container, false);
        return rootView;
    }

However, when I run the app, it loads, and it works just fine, however I get this error:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{co.name.app/co.name.app.CameraEditorActivity}: java.lang.NullPointerException: Layout parameters cannot be null
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

....

 Caused by: java.lang.NullPointerException: Layout parameters cannot be null
        at android.view.View.setLayoutParams(View.java:11470)
        at co.name.app.EditPhotoFragment.onCreateView(EditPhotoFragment.java:89)

Can you advise what the underlying issue is?

2
  • The same error occurs Commented Apr 21, 2015 at 22:41
  • After cleaning and restarting the project its working. Joy.... Commented Apr 22, 2015 at 19:31

1 Answer 1

1

I can not reproduce your error, your code works in my test case.

The Layout parameters error for sure refers to image? Check if all components in R.layout.fragment_edit_photo have layout data.

If so, the underlying issue may be that the fragment's parent container layout might not be a LinearLayout. Also check if your SDK is up to date.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.