Android sensor applications

Source: Internet
Author: User

First, the sensor application android4.0 above

This error java.lang.RuntimeException:Unable to start activity componentinfo{com.sensorget/com.sensorget.mainactivity}: Java.lang.NullPointerException

Cried, seemingly android4.0 above the main thread can not handle too many tasks, the system will force kill. So you should use a sub-thread to update ui!!

 PackageCom.sensorget;Importjava.util.List;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;Importandroid.app.Fragment;ImportAndroid.content.Context;ImportAndroid.hardware.Sensor;ImportAndroid.hardware.SensorManager;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView; @SuppressLint ("Handlerleak") Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); if(Savedinstancestate = =NULL) {Getfragmentmanager (). BeginTransaction (). Add (R.id.container,Newplaceholderfragment ()). commit (); }        NewThread (runnable). Start ();//Thread Execution! } Handler Handler=NewHandler () { Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Bundle Data=Msg.getdata (); String Val= Data.getstring ("Value"); //preparing the UI build for displaying informationTextView txt =(TextView) Findviewbyid (R.ID.TEXTVIEW1);        Txt.settext (Val);    }    }; Runnable Runnable=NewRunnable () {@Override Public voidrun () {Message msg=NewMessage (); Bundle Data=NewBundle (); Data.putstring ("Value", Initview ());            Msg.setdata (data);        Handler.sendmessage (msg);    }    }; PrivateString Initview () {//get the Sensor manager from System servicesSensormanager SM =(Sensormanager) Getsystemservice (Context.sensor_service); //get the full list of sensors from the sensor managerList<sensor> allsensors =sm.getsensorlist (Sensor.type_all); String Str= ""; //shows how many sensors there are//Txt.settext ("detected by the phone has" + allsensors.size () + "sensors, they are: \ n");str + = "detected by the phone has" + allsensors.size () + "sensors, they are: \ n"; //display specific information for each sensor         for(Sensor s:allsensors) {String tempstring= "\ n" + "Device Name:" + s.getname () + "\ n" + "Device version:" + s.getversion () + "\ n" + "Supplier:" + S.getvendor () + "\ n"; System.out.println ("Sensor:" +S.gettype ()); Switch(S.gettype ()) { CaseSensor.TYPE_ACCELEROMETER:str+ = S.gettype () + "Accelerometer Accelerometer" +tempstring;  Break;  CaseSensor.TYPE_AMBIENT_TEMPERATURE:str+ = S.gettype () + "Temperature sensor temperature" +tempstring;  Break;  CaseSensor.TYPE_GRAVITY:str+ = S.gettype () + "Gravity sensor gravity" +tempstring;  Break;  CaseSensor.TYPE_GYROSCOPE:str+ = S.gettype () + "Gyro sensor gyroscope" +tempstring;  Break;  CaseSensor.TYPE_LIGHT:str+ = S.gettype () + "ambient light sensor lights" +tempstring;  Break;  CaseSensor.TYPE_LINEAR_ACCELERATION:str+ = S.gettype () + "linear acceleration sensor linear_accelerometer" +tempstring;  Break;  CaseSensor.TYPE_MAGNETIC_FIELD:str+ = S.gettype () + "electromagnetic field sensor magnetic field" +tempstring;  Break;  CaseSensor.TYPE_PRESSURE:str+ = S.gettype () + "pressure sensor pressure" +tempstring;  Break;  CaseSensor.TYPE_PROXIMITY:str+ = S.gettype () + "distance sensor proximity" +tempstring;  Break;  CaseSensor.TYPE_RELATIVE_HUMIDITY:str+ = S.gettype () + "humidity sensor relative_humidity" +tempstring;  Break;  CaseSensor.TYPE_ROTATION_VECTOR:str+ = S.gettype () + "rotational vector sensor Rotation_vector" +tempstring;  Break; default: Str+ = S.gettype () + "Unknown sensor" +tempstring;  Break; }        }        returnstr; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }    /*** A placeholder fragment containing a simple view. */     Public Static classPlaceholderfragmentextendsFragment { Publicplaceholderfragment () {} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View Rootview=inflater.inflate (R.layout.fragment_main, container,false); returnRootview; }    }}

Android sensor applications

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.