【问题标题】:setPixel() returns a NullPointerExceptionsetPixel() 返回 NullPointerException
【发布时间】:2014-03-04 16:51:34
【问题描述】:

朋友们,当我尝试在我的应用程序中使用 setPixel(x, y, color) 时,它返回 NullPointerException 并停止工作。这是我的代码。请帮助我摆脱错误。提前致谢

decode=(Button)findViewById(R.id.button1);
    decode.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            try
            {
            Bitmap result = BitmapFactory.decodeFile(filePath);



            int x=result.getPixel(10, 10);  
            int pixelAlpha=   Color.alpha(x);
            int red =   65;// represent character A
            int green=    Color.green(x);
            int blue=    Color.blue(x);

            int newPixel=Color.argb(pixelAlpha,red,green,blue);

            result.setPixel(10, 10, newPixel);   


             ImageView myNewImage=(ImageView)findViewById(R.id.imageView2);
             myNewImage.setImageBitmap(result);

            }
            catch(Exception exc)
            {
                Toast.makeText(getBaseContext(), "Exception : "+exc, Toast.LENGTH_LONG).show();
            }
        }
    });

【问题讨论】:

  • 这不会解决您的问题,但请使用 int red = 'A'; 而不是 65。
  • 发布你的日志。在此之前:删除你的 try-catch,这样你就有一个“干净”的异常。
  • 兄弟,如果我删除 try 和 catch ,应用程序将停止工作而没有任何通知,,所以我在这里使用 try 和 catch
  • 如果您删除 try catch 块并且它在没有通知的情况下崩溃,那么您可以打开您的 logcat 并获取堆栈跟踪并与我们分享。

标签: java android bitmap android-image


【解决方案1】:

看起来Bitmap result = BitmapFactory.decodeFile(filePath); 无法正确解码您的文件路径。确保结果不为空并且您的文件路径正确。

【讨论】:

  • 抱歉,我是 android 新手。我如何确保我的结果不为空?我试图在 ImageView() 中打印它,它工作得很好..有没有其他方法来解码 filePath
  • 结果不是空boss。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 2014-01-22
  • 2013-03-08
相关资源
最近更新 更多