我一直致力于在 Android 上制作自己的自定义 ImageView 类。我已经阅读了有关布局、 fragment 、 Activity 和 ImageView 的文档。
我看过关于如何以编程方式创建 ImageView 的 Stack 帖子,但我遇到了一个障碍,我似乎找不到解决方案(阅读搜索上述错误的热门点击并不适用于我的问题)。
我有一个 Activity ,在该 Activity 中我有一个 fragment 。在 fragment 内,我创建了一个 ImageView(它称为 iImage,但它只是一个扩展的 ImageView 类)。
@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;
}
但是,当我运行该应用程序时,它会加载,并且工作正常,但我收到此错误:
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)
您能告诉我们根本问题是什么吗?
最佳答案
我无法重现您的错误,您的代码在我的测试用例中有效。
布局参数错误肯定是指图像吗?检查R.layout.fragment_edit_photo中的所有组件是否都有布局数据。
如果是这样,根本问题可能是 fragment 的父容器布局可能不是 LinearLayout。另请检查您的 SDK 是否是最新的。
关于java.lang.NullPointerException : Layout parameters cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784034/