title: Runtime crash - ebook app with gesture listener and image share
body: <p>i am new to android and i am creating an ebook app that uses the gesture listener and share image. The code doesn't show that anything is wrong, but the app crashes.
  Can someone help me to figure out what was i missing?</p>
  
  <pre><code>public class MainActivity extends Activity implements android.view.View.OnClickListener  {
  
  ImageView page;
  ImageView sound;
  ImageView share;
  View v; 
  private GestureDetector gestureDetector;
  View.OnTouchListener gestureListener;
  private String currentPage="ap";
  private int currentPageNumber=1;
  private MediaPlayer pageTurn;
  private MediaPlayer background;
  private String key = "com.pavle.ebook.key";
  private String keyPlay = "com.pavle.ebook.play";
  private int isPlaying = 0;
  SharedPreferences pref;
  SharedPreferences play;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
      sound = (ImageView) findViewById(R.id.Sound);
      share = (ImageView) findViewById(R.id.Share);
  
  
  
      //Initialize mediaplayer for pageturn and background
      pageTurn = MediaPlayer.create(MainActivity.this,R.raw.page_turn);
      background = MediaPlayer.create(MainActivity.this,R.raw.background);
  
      /*//Set oppacity to the sound image and the share image
      AlphaAnimation alpha = new AlphaAnimation(0.75F, 0.75F);
      alpha.setDuration(0); // Make animation instant
      alpha.setFillAfter(true); // Tell it to persist after the animation ends
      // And then on your layout
      sound.startAnimation(alpha);
      share.startAnimation(alpha);*/
  
      //Set click listener for the views
      sound.setOnClickListener(this);
      share.setOnClickListener(this);
  
      //Load saved page number and isPlaying state
      pref = this.getSharedPreferences("com.pavle.ebook", Context.MODE_PRIVATE);
      play = this.getSharedPreferences("com.pavle.ebook", Context.MODE_PRIVATE);
      currentPageNumber=pref.getInt(key,1);
      isPlaying = play.getInt(keyPlay, 0);
  
      //Set background music if isPlaying is 0
      if(isPlaying==0){
          sound.setImageResource(R.drawable.sound_on);
          background.setLooping(true);
          background = MediaPlayer.create(MainActivity.this,R.raw.background);
          background.start();
          background.setVolume(75, 75);
      }
      else
          sound.setImageResource(R.drawable.sound_off);
  
      //Set the page equal to the page number
      page = (ImageView) findViewById(R.id.Page);
      currentPage="ap"+currentPageNumber;
  
      //First get the resource Id with the string
      int resId = getResources().getIdentifier(currentPage, "drawable", "com.pavle.ebook");
      page.setImageResource(resId);
      page.setScaleType(ScaleType.FIT_CENTER);
  
  
  
      //Set up gesture detector
       gestureDetector = new GestureDetector(this, new MyGestureDetector());
          gestureListener = new View.OnTouchListener() {
              public boolean onTouch(View v, MotionEvent event) {
                  return gestureDetector.onTouchEvent(event);
              }
          };
  
      page.setOnClickListener(MainActivity.this);
      page.setOnTouchListener(gestureListener);
  
  
  
  }
  
  class MyGestureDetector extends SimpleOnGestureListener {
  
      final ViewConfiguration vc = ViewConfiguration.get(getBaseContext());
      final int SWIPE_MIN_DISTANCE = vc.getScaledPagingTouchSlop();
      final int SWIPE_THRESHOLD_VELOCITY = vc.getScaledMinimumFlingVelocity();
      final int SWIPE_MAX_OFF_PATH= vc.getScaledTouchSlop();
  
      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
          try {
              if (Math.abs(e1.getY() - e2.getY()) &gt; SWIPE_MAX_OFF_PATH)
                  return false;
              // right to left swipe
              if(e1.getX() - e2.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) {
                  pageTurn = MediaPlayer.create(MainActivity.this,R.raw.page_turn);
                  if(pageTurn.isPlaying())
                      pageTurn.stop();
                  if(isPlaying==0)
                  pageTurn.start();
                  if(currentPageNumber++&lt;=86){
                      //Page number increases and loads onto the imageView
                    currentPage="ap"+currentPageNumber;
                    int resId = getResources().getIdentifier(currentPage, "drawable", "com.pavle.ebook");
                    page.setImageResource(resId);
                    page.setScaleType(ScaleType.FIT_CENTER);
                  }
                  else{
                      //If it's the last page that is loading, page number is 87 and it will stay that way
                      page.setImageResource(R.drawable.ap87);
                      page.setScaleType(ScaleType.FIT_CENTER);
                      currentPageNumber=87;
                  }
                  pref.edit().putInt(key,currentPageNumber).apply(); 
  
                //left to right swap
              }  else if (e2.getX() - e1.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) {
                  pageTurn = MediaPlayer.create(MainActivity.this,R.raw.page_turn);
                  if(pageTurn.isPlaying())
                      pageTurn.stop();
                  if(isPlaying==0)
                  pageTurn.start();
                  if(--currentPageNumber&gt;1){
                      //Page number decreases and loads onto the imageView
                        currentPage="ap"+currentPageNumber;
                        int resId = getResources().getIdentifier(currentPage, "drawable", "com.pavle.ebook");
                        page.setImageResource(resId);
                        page.setScaleType(ScaleType.FIT_CENTER);
                    }
                    else{
                        //The page number cannot be less than 1
                      page.setImageResource(R.drawable.ap1);
                      page.setScaleType(ScaleType.FIT_CENTER);
                      currentPageNumber=1;
                    }
                  pref.edit().putInt(key,currentPageNumber).apply(); 
                  return true;
              }
          } catch (Exception e) {
              // nothing
          }
          return false;
      }
  
          @Override
      public boolean onDown(MotionEvent e) {
            return true;
      }
  }
  
  public void onClick(View v) {
      // TODO Auto-generated method stub
      switch(v.getId()){
          case R.id.Sound:
              //TODO Create a persistant variable to stop all sounds or turn them on
              if(isPlaying==0){
                  isPlaying=1;
                  sound.setImageResource(R.drawable.sound_off);
                  if(background.isPlaying())
                      background.stop();
                  if(pageTurn.isPlaying())
                      pageTurn.stop();
              }
              else {
                  isPlaying=0;
                  sound.setImageResource(R.drawable.sound_on);
                  if(background.isPlaying())
                      background.stop();
                      background.setLooping(true);
                      background.setVolume(75, 75);
                      background = MediaPlayer.create(MainActivity.this,R.raw.background);
                      background.start();
              }
              play.edit().putInt(keyPlay, isPlaying).apply();
              break;
          case R.id.Share:
              currentPage="ap"+currentPageNumber;
              int resId = getResources().getIdentifier(currentPage, "drawable", "com.pavle.ebook");
              Bitmap icon = BitmapFactory.decodeResource(getResources(), resId);
              Intent share = new Intent(Intent.ACTION_SEND);
              share.setType("image/jpeg");
              ByteArrayOutputStream bytes = new ByteArrayOutputStream();
              icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
              File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
              try {
                  f.createNewFile();
                  FileOutputStream fo = new FileOutputStream(f);
                  fo.write(bytes.toByteArray());
                  fo.close();
              } catch (IOException e) {                       
                      e.printStackTrace();
              }
              share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
              startActivity(Intent.createChooser(share, "Share Image"));
  
              break;
  
      }
  
  }
  @Override
  protected void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      //Load saved page number and isPlaying state
          pref = this.getSharedPreferences("com.pavle.ebook", Context.MODE_PRIVATE);
          play = this.getSharedPreferences("com.pavle.ebook", Context.MODE_PRIVATE);
          currentPageNumber=pref.getInt(key,1);
          isPlaying = play.getInt(keyPlay, 0);
          pageTurn = MediaPlayer.create(MainActivity.this,R.raw.page_turn);
          background = MediaPlayer.create(MainActivity.this,R.raw.background);
          background.setLooping(true);
      if(isPlaying==0){
          if(background.isPlaying())
              background.stop();
          background.setVolume(75, 75);
          background = MediaPlayer.create(MainActivity.this,R.raw.background);
          background.start();
      }
  
  
  }
  
  @Override
  protected void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      if(background.isPlaying())
          background.stop();
      if(pageTurn.isPlaying())
          pageTurn.stop();
      background.release();
      pageTurn.release();
  }
  </code></pre>
  
  <p>When i run it on the emulator, it doesn't even start and gives me the java.lang.RuntimeExeption: Unable to start activity...
  But it's runnable on the phone, and crashes sometimes for no reason.
  Thanks!
  P.S.
  Here is my log</p>
  
  <pre><code>02-09 13:15:42.285: D/skia(750): --- decoder-&gt;decode returned false
  02-09 13:15:42.285: D/AndroidRuntime(750): Shutting down VM
  02-09 13:15:42.285: W/dalvikvm(750): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
  02-09 13:15:42.296: I/dalvikvm(750): Wrote stack traces to '/data/anr/traces.txt'
  02-09 13:15:42.385: E/AndroidRuntime(750): FATAL EXCEPTION: main
  02-09 13:15:42.385: E/AndroidRuntime(750): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pavle.ebook/com.pavle.ebook.MainActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class &lt;unknown&gt;
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.os.Handler.dispatchMessage(Handler.java:99)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.os.Looper.loop(Looper.java:137)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.app.ActivityThread.main(ActivityThread.java:4424)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at java.lang.reflect.Method.invokeNative(Native Method)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at java.lang.reflect.Method.invoke(Method.java:511)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at dalvik.system.NativeStart.main(Native Method)
  02-09 13:15:42.385: E/AndroidRuntime(750): Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class &lt;unknown&gt;
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.view.LayoutInflater.createView(LayoutInflater.java:606)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.app.Activity.setContentView(Activity.java:1835)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at com.pavle.ebook.MainActivity.onCreate(MainActivity.java:46)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.app.Activity.performCreate(Activity.java:4465)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
  02-09 13:15:42.385: E/AndroidRuntime(750):  ... 11 more
  02-09 13:15:42.385: E/AndroidRuntime(750): Caused by: java.lang.reflect.InvocationTargetException
  02-09 13:15:42.385: E/AndroidRuntime(750):  at java.lang.reflect.Constructor.constructNative(Native Method)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.view.LayoutInflater.createView(LayoutInflater.java:586)
  02-09 13:15:42.385: E/AndroidRuntime(750):  ... 25 more
  02-09 13:15:42.385: E/AndroidRuntime(750): Caused by: java.lang.OutOfMemoryError
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.content.res.Resources.loadDrawable(Resources.java:1935)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.widget.ImageView.&lt;init&gt;(ImageView.java:119)
  02-09 13:15:42.385: E/AndroidRuntime(750):  at android.widget.ImageView.&lt;init&gt;(ImageView.java:109)
  02-09 13:15:42.385: E/AndroidRuntime(750):  ... 28 more
  </code></pre>