【问题标题】:NullPointerException when calling Bitmap.createBitmap调用 Bitmap.createBitmap 时出现 NullPointerException
【发布时间】:2020-06-28 20:23:04
【问题描述】:

我正在尝试从视图中绘制位图,因为我想模糊该位图并将其用作后续活动的背景。 Logcat 给我:

NullPointerException at android.graphics.Bitmap.createBitmap(Bitmap.java:484)

我的代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_splash_page);

    ProgressWheel wheel = (ProgressWheel) findViewById(R.id.progress_wheel);
    wheel.setClickable(false);

    TextView touch = (TextView) findViewById(R.id.touch_splash);
    touch.setClickable(false);

    TextView level = (TextView) findViewById(R.id.level_splash);
    level.setClickable(false);


    wheel.setProgress(0.7f);
    wheel.setClickable(true);
    touch.setClickable(true);
    level.setClickable(true);

    RelativeLayout view = (RelativeLayout)findViewById(R.id.viewtocapture);
    ImageView bmImage = (ImageView)findViewById(R.id.imageView);

    view.setDrawingCacheEnabled(true);
    // this is the important code :)
    // Without it the view will have a dimension of 0,0 and the bitmap will be null

    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    view.buildDrawingCache(true);
    view.buildDrawingCache();
    Bitmap b = Bitmap.createBitmap(view.getDrawingCache());      //Line 53
    view.setDrawingCacheEnabled(false); // clear drawing cache

    bmImage.setImageBitmap(b);


    wheel.setProgress(0.0f);

}

这是 Stacktrace/Logcat:

Caused by: java.lang.NullPointerException
        at android.graphics.Bitmap.createBitmap(Bitmap.java:484)
        at com.redstonedevelopers.status_v1.SplashPage.onCreate(SplashPage.java:53)
        at android.app.Activity.performCreate(Activity.java:5047)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)       
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
        at android.app.ActivityThread.access$700(ActivityThread.java:134)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)

我尝试过以下问题的答案: 这里:take a screenshot from the activity 这里:NullPointerException in createBitmap() from View 在这里:android createBitmap NullPointerException

一切都无济于事。我应该做些什么改变和/或做些什么才能让它发挥作用?

-R

【问题讨论】:

  • at com.redstonedevelopers.status_v1.SplashPage.onCreate(SplashPage.java:53) 请显示您的 onCreate 并注明第 53 行。
  • @redstonedev ok 然后放 view.setDrawingCacheEnabled(true);正好在 view.buildDrawingCache();
  • @redstonedev 好的...我有的最后一个解决方案:使用 view.getRootView().getDrawingCache() intsead of view.getDrawingCache()
  • 在第一次实际绘制之前,您不会检测到任何东西。该视图甚至不会在 On Create 中的屏幕上绘制,因此它将为空。您需要使用 TreeViewObserver 并执行所有这些操作。
  • @redstonedev 试试这个答案,如果你愿意,你可以把它放在你自己的方法中。当我需要知道动态视图的大小时,我会使用它,您可以将它用于其他事情,就像您正在尝试做的事情一样。

标签: android bitmap nullpointerexception


【解决方案1】:

试试这个。
这将在实际绘制视图时开始。
然后你应该能够为它保存位图。

-----确保 bmImageview 应该是类级别,以便我们可以在 ViewTreeObserver 触发时使用它们。

 bmImage = (ImageView)findViewById(R.id.imageView);
 view = (RelativeLayout)findViewById(R.id.viewtocapture);
 view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onGlobalLayout() {
            view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            System.out.println(view == null ? "is null" : "not null");
            Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);                
            Canvas c = new Canvas(b);
            view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
            view.draw(c);
            bmImage.setImageBitmap(b);
        }
    });

【讨论】:

  • @redstonedex 使视图成为全局变量。转到您的外部类并执行RelativeLayout 视图;然后设置它 view = (RelativeLayout)findViewById(R.id.viewtocapture);
  • 将位图编辑为我从视图中获取位图的方式
  • @restonedev 请记住,bmImage 也需要是一个全局对象。因为 ViewTreeObserver 不会立即触发,并且在它触发 bmImage 时可能为空。
  • 你不是说全局,你是说类级别。全局具有非常明确的含义 - 全局具有与应用程序相同的范围、可见性和生命周期。
  • @redstonedev 嘿,开发者执行我的编辑并看到它的视图是 null 它不应该是
【解决方案2】:

直到onCreate() 之后才测量、布局和渲染视图树。

您可以像这样使用树视图布局观察器。

public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);

     // inflate your main layout here (use RelativeLayout or whatever your root ViewGroup type is
     LinearLayout mainLayout = (LinearLayout ) this.getLayoutInflater().inflate(R.layout.activity_splash_page, null); 

     // set a global layout listener which will be called when the layout pass is completed and the view is drawn
     mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
     new ViewTreeObserver.OnGlobalLayoutListener() {
          public void onGlobalLayout() {
               //Remove the listener before proceeding
               if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
               } else {
                    mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
               }

               // do your thing here
          }
     }
 );

 setContentView(mainLayout);

【讨论】:

    【解决方案3】:

    我使用这样的代码在画布对象上绘制视图。 不确定这是否适用于您的情况。

    picturePreLoad = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
       Canvas canvas = new Canvas(picturePreLoad);
       MyView.draw(canvas);
    

    【讨论】:

      【解决方案4】:

      就我而言,问题是位图文件默认复制到Drawable V24,而不是Drawable。 为了能够同时看到这两个文件夹,在左上角,将设置从 Android 更改为 Project

      然后转到app.src.main.res,在那里你可以看到Drawable V24Drawable。 您只需从Drawable V24 中删除位图并将它们添加到Drawable

      【讨论】:

        猜你喜欢
        • 2013-12-29
        • 2015-02-13
        • 2012-01-21
        • 2012-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-16
        • 1970-01-01
        相关资源
        最近更新 更多