title: Java Android - Can't use OnClick functions outside of CustomView inside CustomView
body: <p>I am currently making a game which expands off of the tutorial found here:
  <a href="http://www.edu4java.com/en/androidgame/androidgame.html" rel="nofollow">http://www.edu4java.com/en/androidgame/androidgame.html</a></p>
  
  <p>I currently have a screen layout that looks like the following:
  <a href="http://postimg.org/image/ghib8fb87/" rel="nofollow">http://postimg.org/image/ghib8fb87/</a></p>
  
  <p>As you can see, the directional keypad and the difficulty radio buttons are outside of my custom view.</p>
  
  <p><strong>EDIT</strong>: Attempted to use SharedPreferences:</p>
  
  <p>In my MainActivity I have functions for the onClick of the RadioButtons and Directional keypad (not yet created):</p>
  
  <pre><code>import java.text.DecimalFormat;
  import java.util.Timer;
  import java.util.TimerTask;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.Window;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
      /** Called when the activity is first created. */
      private AppPreferences _appPrefs;
      int hour = 0;
      int minute = 0;
      int second = 0;
      TextView TimeAliveCounter;
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.activity_main);
          timer();
      }
  
      public void runEasy(View view){
          _appPrefs.saveSmsBody("1");
  
      }
  
      public void runMedium(View view){
          _appPrefs.saveSmsBody("2");
      }
  
      public void runHard(View view){
          _appPrefs.saveSmsBody("3");
  
      }
  
      private void timer() {
  
          int locMilSec = 0;
          int locSec = 0;
          int locMin = 0;
          int locHr = 0;
          DecimalFormat format = new DecimalFormat("00");
          String formatMilliSecond = format.format(locMilSec);
          String formatSecond = format.format(locSec);
          String formatMinute = format.format(locMin);
          String formatHour = format.format(locHr);
  
          millisecond = Integer.parseInt(formatMilliSecond);
          second = Integer.parseInt(formatSecond);
          minute = Integer.parseInt(formatMinute);
          hour = Integer.parseInt(formatHour);
  
          Timer T = new Timer();
          TimeAliveCounter = (TextView) findViewById(R.id.TimeAlive);
          T.scheduleAtFixedRate(new TimerTask() {
              @Override
              public void run() {
                  runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          TimeAliveCounter.setText(hour + ":"+ minute + ":"+ second + ":" + millisecond);
                          second++;
                          if(second &gt; 59){
                              second = 0;
                              minute = minute + 1;                          
                          }
                      }
                  });
              }
          }, 1000, 1000);
      }
  }
  </code></pre>
  
  <p>My AppPreferences Class:</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.content.SharedPreferences.Editor;
  
  public class AppPreferences {
      public static final String KEY_DIFFICULTY = "difficulty";
      private static final String APP_SHARED_PREFS = AppPreferences.class.getSimpleName(); 
      private SharedPreferences _sharedPrefs;
      private Editor _prefsEditor;
  
      public AppPreferences(Context context) {
          this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
          this._prefsEditor = _sharedPrefs.edit();
      }
  
      public String getDifficulty() {
          return _sharedPrefs.getString(KEY_DIFFICULTY, "");
      }
  
      public void saveSmsBody(String text) {
          _prefsEditor.putString(KEY_DIFFICULTY, text);
          _prefsEditor.commit();
      }
  }
  </code></pre>
  
  <p>My GameView class:</p>
  
  <pre><code>    import java.util.ArrayList;
  import java.util.List;
  
  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.util.AttributeSet;
  import android.view.MotionEvent;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  
  
  public class GameView extends SurfaceView {
         private GameLoopThread gameLoopThread;
         private List&lt;Sprite&gt; sprites = new ArrayList&lt;Sprite&gt;();
         private List&lt;tempSprite&gt; temps = new ArrayList&lt;tempSprite&gt;();
         private long lastClick;
         private Bitmap bmpBlood;
         private AppPreferences _appPrefs;
  
         public GameView(Context context, AttributeSet attributeSet) {
  
               super(context);
               _appPrefs = new AppPreferences(context);
               final String difficulty = _appPrefs.getDifficulty();
               gameLoopThread = new GameLoopThread(this);
               getHolder().addCallback(new SurfaceHolder.Callback() {
  
                      @Override
                      public void surfaceDestroyed(SurfaceHolder holder) {
                             boolean retry = true;
                             gameLoopThread.setRunning(false);
                             while (retry) {
                                    try {
                                          gameLoopThread.join();
                                          retry = false;
                                    } catch (InterruptedException e) {}
                             }
                      }
  
  
  
                      @Override
                      public void surfaceCreated(SurfaceHolder holder) {
                          createSprites(difficulty);    
                          gameLoopThread.setRunning(true);
                          gameLoopThread.start();
                      }
  
                      @Override
                      public void surfaceChanged(SurfaceHolder holder, int format,
                                    int width, int height) {
                      }
               });
               bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.image);
         }
  
         private void createSprites(final String difficulty) {
  
             if ("1".equals(difficulty))  // easy
             {
                 new Thread(new Runnable() {
                  public void run() {
                      for (int z =0; z&lt;20; z++ )
                      try
                          {
                          Thread.sleep(5000); 
                          sprites.add(createSprite(R.drawable.image));
                          z++;
  
                          }
                      catch(InterruptedException e)
                          {
                          e.printStackTrace();
                          }
                  }
              }).start();
             }
  
             else if ("2".equals(difficulty)) // medium
             {
                 new Thread(new Runnable() {
                  public void run() {
                      for (int z =0; z&lt;40; z++ )
                      try
                          {
                          Thread.sleep(3000); 
                          sprites.add(createSprite(R.drawable.image));
                          z++;
  
                          }
                      catch(InterruptedException e)
                          {
                          e.printStackTrace();
                          }
                  }
              }).start();
             }
  
             else if ("3".equals(difficulty)) // hard" +
  
             {
                 new Thread(new Runnable() {
                  public void run() {
                      for (int z =0; z&lt;60; z++ )
                      try
                          {
                          Thread.sleep(1000); 
                          sprites.add(createSprite(R.drawable.zombie_sprite1));
                          z++;
  
                          }
                      catch(InterruptedException e)
                          {
                          e.printStackTrace();
                          }
                  }
              }).start();
             } 
  
             else {
                  sprites.add(createSprite(R.drawable.zombie_sprite1));
                  sprites.add(createSprite(R.drawable.zombie_sprite2));
                  sprites.add(createSprite(R.drawable.zombie_sprite3));
                  sprites.add(createSprite(R.drawable.zombie_sprite4));
                  sprites.add(createSprite(R.drawable.zombie_sprite1));
                  sprites.add(createSprite(R.drawable.zombie_sprite2));
                  sprites.add(createSprite(R.drawable.zombie_sprite3));
                  sprites.add(createSprite(R.drawable.zombie_sprite4));
                  sprites.add(createSprite(R.drawable.zombie_sprite1));
                  sprites.add(createSprite(R.drawable.zombie_sprite2));
                  sprites.add(createSprite(R.drawable.zombie_sprite3));
                  sprites.add(createSprite(R.drawable.zombie_sprite4));
             }
          }
  
  
         private Sprite createSprite(int resouce) {
               Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
               return new Sprite(this, bmp);
         }
  
         @Override
         protected void onDraw(Canvas canvas) {
  
  
             Resources res = getResources();
             Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.graveyard_background);
  
  
              canvas.drawBitmap(background, 0, 0, null);
  
               for (int i = temps.size() - 1; i &gt;= 0; i--) {
                      temps.get(i).onDraw(canvas);
               }
               for (Sprite sprite : sprites) {
                      sprite.onDraw(canvas);
               }
         }
  
  
  
         @Override
         public boolean onTouchEvent(MotionEvent event) {
               /**if (System.currentTimeMillis() - lastClick &gt; 300) {
                      lastClick = System.currentTimeMillis();
                      float x = event.getX();
                      float y = event.getY();
                      synchronized (getHolder()) {
                             for (int i = sprites.size() - 1; i &gt;= 0; i--) {
                                    Sprite sprite = sprites.get(i);
                                    if (sprite.isCollision(x, y)) {
                                          sprites.remove(sprite);
                                          temps.add(new tempSprite(temps, this, x, y, bmpBlood));
                                          break;
                                    }
                             }
                      }
               }*/
               return true;
         }          
  }
  </code></pre>
  
  <p>My GameLoopThread class:</p>
  
  <pre><code>    import android.graphics.Canvas;
  
  public class GameLoopThread extends Thread {
         static final long FPS = 10;
         private GameView view;
         private boolean running = false;
  
         public GameLoopThread(GameView view) {
               this.view = view;
         }
  
         public void setRunning(boolean run) {
               running = run;
         }
  
         @Override
         public void run() {
               long ticksPS = 1000 / FPS;
               long startTime;
               long sleepTime;
               while (running) {
                      Canvas c = null;
                      startTime = System.currentTimeMillis();
                      try {
                             c = view.getHolder().lockCanvas();
                             synchronized (view.getHolder()) {
                                    view.onDraw(c);
                             }
                      } finally {
                             if (c != null) {
                                    view.getHolder().unlockCanvasAndPost(c);
                             }
                      }
                      sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
                      try {
                             if (sleepTime &gt; 0)
                                    sleep(sleepTime);
                             else
                                    sleep(10);
                      } catch (Exception e) {}
               }
         }
  }
  </code></pre>
  
  <p>I am currently trying to be able to get the radio buttons to change the difficulty of the game but currently on clicking the radio buttons it is crashing.</p>