Android Development shake-shake function

Source: Internet
Author: User


==================== Problem Description ====================
Here is the code that I shook to implement
Public class yao extends activity implements sensoreventlistener{ edittext  tv = (EditText) Findviewbyid (R.ID.EDITTEXT1);  button button = (Button) Findviewbyid ( R.id.button1);  sensormanager sensormanager = null; vibrator vibrator =  null;  @Overrideprotected  void oncreate (bundle savedinstancestate)  { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);if  ( ANDROID.OS.BUILD.VERSION.SDK_INT > 9)  {             StrictMode.ThreadPolicy policy = new  StrictMode.ThreadPolicy.Builder (). Permitall (). Build ();             strictmode.setthreadpolicy (Policy);        }  Button.setonclicklistener (New onclicklistener ()   {@Overridepublic  void oNclick (VIEW V)  {// todo auto-generated method stubtv.settext ("initial text");}     });sensormanager =  (Sensormanager)  getsystemservice (SENSOR_SERVICE); vibrator =  (Vibrator)  getsystemservice (Service.vibrator_service);} @Override  protected void onpause ()  {  super.onpause ();   Sensormanager.unregisterlistener (This); }   @Override  protected void onresume ()  {  super.onresume ();   sensormanager.registerlistener (this,   Sensormanager.getdefaultsensor (Sensor.type_accelerometer),   sensormanager.sensor_delay_normal);  }   @Override  public void onaccuracychanged (sensor sensor, int  Accuracy)  {  //when the sensor accuracy is changed,do nothing. }   @Override  public  Void onsensorchanged (sensorevent event)  {   int sensortypE = event.sensor.gettype ();   //values[0]:x axis, values[1]:y axis, values[2]:z axis   float[]  values = event.values;  if  (Sensortype == sensor.type_accelerometer)   {   if  (Math.Abs (values[0])  > 17 | |  math.abs (values[1])  > 17 | |  math     .abs (values[2])  > 17)    {//     LOG.D ("sensor x ",  "============ values[0] = "  + values[0] );//    LOG.D ("sensor y ",  "============ values[1] = "   + values[1]);//    LOG.D ("sensor z ",  "============ values[2]  =  " + values[2");     tv.settext ("Shake!!! Successfully");     //shake mobile phone, and then accompanied by vibration hint ~~    vibrator.vibrate ($);    }    }&nBSP;}} 

Why does the program run with a hint that the program is unresponsive and then quits? I read the code on the Internet is so written, where does the problem arise? High distribution, please help the master
==================== Solution 1====================
Control initialization cannot be placed at the top of the
EditText TV = (EditText) Findviewbyid (R.ID.EDITTEXT1); Button button = (button) Findviewbyid (R.id.button1);

This needs to be
Setcontentview (R.layout.activity_main);
Called after
==================== Solution 2====================
It seems that LZ is a novice ah, later to study hard
Reference 1 Floor guoyoulei520 's reply:
control initialization cannot be placed at the top of the
EditText TV = (EditText) Findviewbyid (R.ID.EDITTEXT1); Button button = (button) Findviewbyid (R.id.button1);

This needs to be
Setcontentview (R.layout.activity_main);
Called after

==================== Solution 3====================
Do a lot of computational work in the UI thread.

Android Development shake-shake function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.