1

Main Activity Oncreate Method whenever i try to run this code it give me this error although i have added runonui Thread . but it is still not working any help will be appreciated . i have searched for this a lot but really didn't find anything

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

       dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        DevicePolicyAdmin = new ComponentName(this,
                Dprcv.class);

  //      btntake =(Button)findViewById(R.id.takepicture);
        truitonAdminEnabledCheckbox = (CheckBox) findViewById(R.id.checkBox);
//start

    //    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.control, null);
        ViewGroup.LayoutParams layoutParamsControl
                = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);


            final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
            buttonTakePicture.setOnClickListener(new Button.OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                    camera.takePicture(myShutterCallback,
                            myPictureCallback_RAW, myPictureCallback_JPG);
                }


            });
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);

        Thread tr = new Thread(){
            public void run(){
                try{

                    final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);

                    sleep(5000);
                    buttonTakePicture.performClick();

                }
                catch (InterruptedException ex)
                {
                    ex.printStackTrace();
                }
            }
        };
        tr.start();



            }
        });

and this is logcat

04-11 15:19:49.629    6343-6360/com.ahmed.raja.wrongclick E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-4119
    Process: com.ahmed.raja.wrongclick, PID: 6343
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
            at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6669)
            at android.view.ViewRootImpl.playSoundEffect(ViewRootImpl.java:5642)
            at android.view.View.playSoundEffect(View.java:17278)
            at android.view.View.performClick(View.java:4462)
            at com.ahmed.raja.wrongclick.MainActivity$2$1.run(MainActivity.java:107)
Brendom
  • 123
  • 1
  • 10
  • From what I infer, the enclosing runnable runs on UI thread (as you've used runOnUiThread()) but within that runnable's run method you're creating another thread (Thread tr), and trying to access UI element in it. Hence, you're getting this exception. – gaurav jain Apr 11 '15 at 10:33

2 Answers2

1
Thread tr = new Thread(){
        public void run(){
            try{
                final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
                sleep(5000);
                buttonTakePicture.performClick();
            }
            catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    };

performClick has to be called on the ui thread, that's the reason why you are getting the exception. If you want to simulate 5 secs waiting before call performClick, you can use the button's handler and its postDelayed method:

final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.postDelayed(new Runnable() {
    public void run() {
         buttonTakePicture.performClick();
    }
}, 5000);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

This is because you start new thread in runOnUiThread method when call tr.start(); This means that this code:

                    final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);

                sleep(5000);
                buttonTakePicture.performClick();

runs not in ui thread. As i understand your code you want to perform click delayed. You can do it in the following way:

buttonTakePicture.postDelayed
x90
  • 2,140
  • 15
  • 25