android - 调用 Bitmap.createBitmap 时出现 NullPointerException

标签 android bitmap nullpointerexception

我正在尝试从 View 中绘制位图,因为我想模糊该位图并将其用作后续 Activity 的背景。 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

最佳答案

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

-----确保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);
        }
    });

关于android - 调用 Bitmap.createBitmap 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30220262/

相关文章:

android - 如何在android中将 Logo 设置为QR码中间

java - Java 14中的NullPointerException与之前的版本有何不同?

java - 我的代码以某种方式返回 null,我不知道为什么

android通过wifi连接接收和发送数据到硬件

android - 如何解决Android中的java.lang.OutOfMemoryError故障

Android 位图 setPixel 无法正常工作? (设置值,然后读取不同的值)

java - eclipse中GUI界面异常

android - osmdroid MapTileDownloader 现在显示 403 禁止作为 HTTP 响应

android - 如何更改 ANDROID 模拟器 UUID?

android - MediaPlayer 在带有 R.raw 音频的 setDataSource 上崩溃