]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/commitdiff
Cover exception when no preview image view is found
authorMichal Horn <hornmich@fel.cvut.cz>
Tue, 10 Feb 2015 15:44:05 +0000 (16:44 +0100)
committerMichal Horn <hornmich@fel.cvut.cz>
Tue, 10 Feb 2015 15:44:05 +0000 (16:44 +0100)
QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java

index 79ee599bf9cafbc4b408a962787cfad06b1f7b27..7d8ee0fecb013014b0709cd33ff1293d76209d58 100644 (file)
@@ -30,9 +30,15 @@ public class PreviewActivity extends ActionBarActivity implements ViewTreeObserv
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_preview);
                mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
-               mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
-               Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
-               mPreviewImgObserver.addOnGlobalLayoutListener(this);
+               if (mPreviewImg != null) {
+                       mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
+                       Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
+                       mPreviewImgObserver.addOnGlobalLayoutListener(this);
+               }
+               else {
+                       Log.e(TAG, "ImageView for preview image could not be found in the resources.");
+                       mPreviewImgObserver = null;
+               }
 
                Intent intent = getIntent();
                mComponentId = intent.getStringExtra("COMPONENT_ID");
@@ -57,7 +63,7 @@ public class PreviewActivity extends ActionBarActivity implements ViewTreeObserv
        protected void onDestroy() {
                super.onDestroy();
                Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
-               if (mPreviewImgObserver.isAlive()) {
+               if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
                        mPreviewImgObserver.removeOnGlobalLayoutListener(this);
                }
        }
@@ -86,27 +92,22 @@ public class PreviewActivity extends ActionBarActivity implements ViewTreeObserv
 
        @Override
        public void onGlobalLayout() {
-               if (mPreviewImg != null) {
-                       try {
-                               String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
-                               Log.i(TAG, "Path to component files: " + picturePath);
+               try {
+                       String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
+                       Log.i(TAG, "Path to component files: " + picturePath);
 
-                               MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
-                               MuPDFCore.Cookie cookie = core.new Cookie();
-                               int previewW = mPreviewImg.getWidth();
-                               int previewH = mPreviewImg.getHeight();
-                               Bitmap.Config conf = Bitmap.Config.ARGB_8888;
-                               Bitmap previewBitmap = Bitmap.createBitmap(previewW, previewH, conf);
-                               core.updatePage(previewBitmap, 0, previewW, previewH, 0, 0, previewW, previewH, cookie);
-                               mPreviewImg.setImageBitmap(previewBitmap);
-                       } catch (Exception e) {
-                               Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
-                               toast.show();
-                               e.printStackTrace();
-                       }
-               }
-               else {
-                       Log.e(TAG, "ImageView for preview image could not be found in the resources.");
+                       MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
+                       MuPDFCore.Cookie cookie = core.new Cookie();
+                       int previewW = mPreviewImg.getWidth();
+                       int previewH = mPreviewImg.getHeight();
+                       Bitmap.Config conf = Bitmap.Config.ARGB_8888;
+                       Bitmap previewBitmap = Bitmap.createBitmap(previewW, previewH, conf);
+                       core.updatePage(previewBitmap, 0, previewW, previewH, 0, 0, previewW, previewH, cookie);
+                       mPreviewImg.setImageBitmap(previewBitmap);
+               } catch (Exception e) {
+                       Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
+                       toast.show();
+                       e.printStackTrace();
                }
        }
 }