Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法拍摄布局的屏幕截图_Java_Android_Android Layout - Fatal编程技术网

Java 无法拍摄布局的屏幕截图

Java 无法拍摄布局的屏幕截图,java,android,android-layout,Java,Android,Android Layout,我正在尝试在我的应用程序中拍摄一个布局的屏幕截图。但当我按下屏幕截图按钮时,应用程序崩溃了 日志: 我使用了一个助手类来获得更干净的代码,这是第298行周围的助手代码: View u = act.findViewById(id); u.setDrawingCacheEnabled(true); Linear

我正在尝试在我的应用程序中拍摄一个布局的屏幕截图。但当我按下屏幕截图按钮时,应用程序崩溃了

日志:

我使用了一个助手类来获得更干净的代码,这是第298行周围的助手代码:

                View u = act.findViewById(id);
                u.setDrawingCacheEnabled(true);                                                
                LinearLayout z = (LinearLayout) act.findViewById(id);
                int totalHeight = z.getHeight();
                int totalWidth = z.getWidth();
                u.layout(0, 0, totalWidth, totalHeight);    
                u.buildDrawingCache(true);
298-->          Bitmap b = Bitmap.createBitmap(u.getDrawingCache()); 
                u.setDrawingCacheEnabled(false);
                String filePath = Environment.getExternalStorageDirectory()
                        + File.separator + st1;
                File myPath = new File(filePath);
                FileOutputStream fos = null;
这是一个奇怪的情况,因为如果我在像三星s4这样的高级手机上测试它,它会崩溃,但如果我在像HTC wildfire s这样的低级手机上测试它,它工作得很好

有什么想法吗

编辑:根据Goran注释,我检查了以这种方式返回null的内容:

                View u = act.findViewById(id);               
                LinearLayout z = (LinearLayout) act.findViewById(id);
                u.setDrawingCacheEnabled(true);
                int totalHeight = z.getHeight();
                int totalWidth = z.getWidth();
                u.layout(0, 0, totalWidth, totalHeight);    
                u.buildDrawingCache(true);
                if (u.getDrawingCache() == null) {
                    Toast.makeText(act.getApplicationContext(), "DrawingCache is Null",
                            Toast.LENGTH_LONG).show();
                } else {
                    Bitmap b = Bitmap.createBitmap(u.getDrawingCache()); 
                    u.setDrawingCacheEnabled(false);
                    String filePath = Environment.getExternalStorageDirectory()
                            + File.separator + st1;
                    File myPath = new File(filePath);
在野火S中,它起作用了,但在s4中,我得到了DrawingCatch是空的Toast

那么如何防止获取空值呢?

感谢您的回答,我使用以下代码解决了我的问题:

CustomView view = (CustomView) findViewById(R.id.customViewId);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), 
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 
Canvas bitmapHolder = new Canvas(bitmap); 
view.draw(bitmapHolder);

放一些日志,看看什么是空的。我怀疑这就是问题所在:u.getDrawingCache,u为null。如果为null,我该怎么办?如果为null,它应该在298上方崩溃2行,在这一行u.layout0,0,totalWidth,totalHeight;我不认为u是空的。u.getDrawingCache此方法的结果可能是空的请参阅此线程:
CustomView view = (CustomView) findViewById(R.id.customViewId);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), 
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 
Canvas bitmapHolder = new Canvas(bitmap); 
view.draw(bitmapHolder);