0

ErrorMessage: Can't create handler inside thread that has not called Looper.prepare()

I'm creating a game that that uses openGL. I have a main activity class that has 3 clickable buttons, one of which will take you to the openGL activity. The problem is when I run app on my device it shuts down immediately. In my openGL activity I'm implementing SensorEventListener and using accelerometer. I've looked into threads but im still a beginner with them so I wasn't able to fix my code.

Main

public class Main extends Activity {

    Handler handler = new Handler(){
        @Override
    public void handleMessage(Message msg){

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

    }

    public void PlayGame(View v) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                Looper.prepare();

                    synchronized (this) {
                        try {
                            Intent intent = new Intent(Main.this,OpenGLActivity.class);
            startActivity(intent);

                }catch(Exception e){
                            Log.e("main", "error");
                        }

        }
        handler.sendEmptyMessage(0);
                Looper.loop();
    }
        };
        Thread myThread = new Thread(r);
        myThread.start();


    }

OpenGLActivity

public class OpenGLActivity extends Activity implements SensorEventListener{
    private Sensor mySensor;
    private SensorManager mySensorManager;
    private MyGLRenderer renderer;
    private GLSurfaceView view;

    float x, y;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        x = y = 0;

        // Get an instance of the SensorManager
        mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

        if(mySensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0){
           mySensor = mySensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
           mySensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
        //set app to full screen and keep screen on
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        renderer = new MyGLRenderer();
        view = new GLSurfaceView(this);
        view.setRenderer(renderer);
        view.onResume();
        setContentView(view);
    }

   @Override
    protected void onResume() {
       mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        //   enables the sensor when the activity is resumed, ask for 10 ms updates.
       mySensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);
                //if the focus to the game is restored then resume game
       super.onResume();
        view.onResume();
    }

    @Override
    protected void onPause() {
        // turn our sensor off when the activity is paused
        mySensorManager.unregisterListener(this);
         //if activity loses focus then pause the game
        super.onPause();
        view.onPause();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        //checks the sensory event to ensure its to type im looking for
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            x = event.values[0];
            y = event.values[1];
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
genpfault
  • 51,148
  • 11
  • 85
  • 139
will
  • 75
  • 1
  • 7
  • This will be quickly closed as a duplicate of the many existing posts on the same subject. As with those, you must push your UI updates back to the UI thread using one of the various mechanisms. – Chris Stratton Mar 13 '15 at 12:45
  • @ChrisStratton Im new with handlers and threads. I know there are many duplicates of this question but wasn't sure how to apply them to my code. Could you email a solution then I could delete this question? – will Mar 13 '15 at 12:56
  • The stack trace of your error which you should obtain from logcat will show you which part of your code is erroneously trying to manipulate the UI from the background. Wrap that part in one of the solutions provided at the linked duplicate. – Chris Stratton Mar 13 '15 at 14:37

0 Answers0