标签:des android style blog io color ar os java
一、传感器应用 android4.0以上
就这种错误java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sensorget/com.sensorget.MainActivity}: java.lang.NullPointerException
哭了,貌似android4.0以上 主线程 不能处理太多任务,系统会强制kill。 所以应采用子线程进行更新ui!!
package com.sensorget; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @SuppressLint("HandlerLeak") public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } new Thread(runnable).start();// 线程执行! } Handler handler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); Bundle data = msg.getData(); String val = data.getString("value"); // 准备显示信息的UI组建 TextView txt = (TextView) findViewById(R.id.textView1); txt.setText(val); } }; Runnable runnable = new Runnable() { @Override public void run() { Message msg = new Message(); Bundle data = new Bundle(); data.putString("value", initView()); msg.setData(data); handler.sendMessage(msg); } }; private String initView() { // 从系统服务中获得传感器管理器 SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); // 从传感器管理器中获得全部的传感器列表 List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL); String str = ""; // 显示有多少个传感器 // txt.setText("经检测该手机有" + allSensors.size() + "个传感器,他们分别是:\n"); str += "经检测该手机有" + allSensors.size() + "个传感器,他们分别是:\n"; // 显示每个传感器的具体信息 for (Sensor s : allSensors) { String tempString = "\n" + " 设备名称:" + s.getName() + "\n" + " 设备版本:" + s.getVersion() + "\n" + " 供应商:" + s.getVendor() + "\n"; System.out.println("传感器:" + s.getType()); switch (s.getType()) { case Sensor.TYPE_ACCELEROMETER: str += s.getType() + " 加速度传感器accelerometer" + tempString; break; case Sensor.TYPE_AMBIENT_TEMPERATURE: str += s.getType() + " 温度传感器temperature" + tempString; break; case Sensor.TYPE_GRAVITY: str += s.getType() + " 重力传感器gravity" + tempString; break; case Sensor.TYPE_GYROSCOPE: str += s.getType() + " 陀螺仪传感器gyroscope" + tempString; break; case Sensor.TYPE_LIGHT: str += s.getType() + " 环境光线传感器light" + tempString; break; case Sensor.TYPE_LINEAR_ACCELERATION: str += s.getType() + " 线性加速度传感器linear_accelerometer" + tempString; break; case Sensor.TYPE_MAGNETIC_FIELD: str += s.getType() + " 电磁场传感器magnetic field" + tempString; break; case Sensor.TYPE_PRESSURE: str += s.getType() + " 压力传感器pressure" + tempString; break; case Sensor.TYPE_PROXIMITY: str += s.getType() + " 距离传感器proximity" + tempString; break; case Sensor.TYPE_RELATIVE_HUMIDITY: str += s.getType() + " 湿度传感器relative_humidity" + tempString; break; case Sensor.TYPE_ROTATION_VECTOR: str += s.getType() + " 旋转矢量传感器rotation_vector" + tempString; break; default: str += s.getType() + " 未知传感器" + tempString; break; } } return str; } @Override public boolean onCreateOptionsMenu(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 boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } }
标签:des android style blog io color ar os java
原文地址:http://www.cnblogs.com/MicroHao/p/4086729.html