From: Michal Horn Date: Tue, 10 Feb 2015 15:44:05 +0000 (+0100) Subject: Cover exception when no preview image view is found X-Git-Url: http://rtime.felk.cvut.cz/gitweb/hornmich/skoda-qr-demo.git/commitdiff_plain/0910ca5e536102503683afb3966956c0cbdebff6 Cover exception when no preview image view is found --- diff --git a/QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java b/QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java index 79ee599..7d8ee0f 100644 --- a/QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java +++ b/QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java @@ -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(); } } }