(1)error opening trace file: No such file or directory (2)

这是写的第一个程序就出现的问题,而且查询代码没有发现错误。google后得出结论:模拟器版本和android的API版本不对应,相应的进行修改就行。

(2)出现java.lang.NumberFormatException: unable to parse 'null' as integer



出现问题查出错误出在上面代码中intent传回来的值有可能是null,就会产生转换的错误,最终修改方案是加入异常处理机制,就是使用try and catch。修改后程序运行正常。代码如下:

public class otheractivity extends Activity{

private TextView textview2 = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_other);

textview2 = (TextView)findViewById(R.id.TextView2);

int oneint = 0;

int twoint = 0;

Intent intent2 = getIntent();

String onestr = intent2.getStringExtra("one");

String twostr = intent2.getStringExtra("two");

try{

oneint = Integer.parseInt(onestr);

}

catch(Exception e){}

try{

twoint = Integer.parseInt(twostr);

}

catch (Exception e) {

// TODO: handle exception

}

int res = oneint + twoint;

String resstr = String.valueOf(res);

textview2.setText(resstr);

}

}

问题就迎刃而解了

(3)用一个监听器实现多个按钮的onclick监听。

示例代码如下:

public class SensorActivity extends Activity {

private Button button1 = null;

private Button button2 = null;

private Button button3 = null;

private Button button4 = null;

private Button button5 = null;

private Button button6 = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_sensor);

button1 = (Button)findViewById(R.id.button1);

button2 = (Button)findViewById(R.id.button2);

button3 = (Button)findViewById(R.id.button3);

button4 = (Button)findViewById(R.id.button4);

button5 = (Button)findViewById(R.id.button5);

button6 = (Button)findViewById(R.id.button6);

button1.setOnClickListener(new MyButtonlistener());

button2.setOnClickListener(new MyButtonlistener());

button3.setOnClickListener(new MyButtonlistener());

button4.setOnClickListener(new MyButtonlistener());

button5.setOnClickListener(new MyButtonlistener());

button6.setOnClickListener(new MyButtonlistener());

}

class MyButtonlistener implements OnClickListener {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

int i[] = new int[6];

for(int j = 0;j<6;j++){

i[j]=j;

}

HashMap<Button,Integer> bf = new HashMap<Button, Integer>(6);

bf.put(button1, i[0]);

bf.put(button2, i[1]);

bf.put(button3, i[2]);

bf.put(button4, i[3]);

bf.put(button5, i[4]);

bf.put(button6, i[5]);

//Intent intent[] = new Intent[6];

Intent intent1 = new Intent();

Intent intent2 = new Intent();

Intent intent3 = new Intent();

Intent intent4 = new Intent();

Intent intent5 = new Intent();

Intent intent6 = new Intent();

//intent[0].setClass(SensorActivity.this, Sensor1.class);

//intent[1].setClass(SensorActivity.this, Sensor2.class);

//intent[2].setClass(SensorActivity.this, Sensor3.class);

//intent[3].setClass(SensorActivity.this, Sensor4.class);

//intent[4].setClass(SensorActivity.this, Sensor5.class);

//intent[5].setClass(SensorActivity.this, Sensor6.class);

intent1.setClass(SensorActivity.this, Sensor1.class);

intent2.setClass(SensorActivity.this, Sensor2.class);

intent3.setClass(SensorActivity.this, Sensor3.class);

intent4.setClass(SensorActivity.this, Sensor4.class);

intent5.setClass(SensorActivity.this, Sensor5.class);

intent6.setClass(SensorActivity.this, Sensor6.class);

Button button = (Button)v;

int num = bf.get(button);

switch (num) {

case 0:

SensorActivity.this.startActivity(intent1);

break;

case 1:

SensorActivity.this.startActivity(intent2);

break;

case 2:

SensorActivity.this.startActivity(intent3);

break;

case 3:

SensorActivity.this.startActivity(intent4);

break;

case 4:

SensorActivity.this.startActivity(intent5);

break;

case 5:

SensorActivity.this.startActivity(intent6);

break;

default:

break;

}

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// TODO Auto-generated method stub

menu.add(0, 1, 1, R.string.exit);

menu.add(0, 2, 2, R.string.about);

return super.onCreateOptionsMenu(menu);

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// TODO Auto-generated method stub

if(item.getItemId()==1){

finish();

}

return super.onOptionsItemSelected(item);

}

}

关键在于用hashmap将控件与整数对应,然后进行选择。

(4)在同一个activity中使用两个或两个以上的传感器。
在同一个activity中使用多个传感器时,只需要声明一个sensormanager就可以了。但是每个传感器实例都需要单独声明。

监听器可以只使用一个监听器。

具体示例如下:(以两个传感器为例)

private SensorManager sm_gyroscope;

private Sensor my_gyroscope;

private Sensor my_rotation;

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.sensor4)

sm_gyroscope = (SensorManager)getSystemService(SENSOR_SERVICE);

my_gyroscope = sm_gyroscope.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

my_rotation = sm_gyroscope.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

}

private class Mysensorlistener implements SensorEventListener{

@Override

public void onAccuracyChanged(Sensor sensor, int accuracy) {

// TODO Auto-generated method stub

}

int temp1 = 0,temp2 = 0;

@Override

public void onSensorChanged(SensorEvent event) {

// TODO Auto-generated method stub

if(event.sensor.getType() == Sensor.TYPE_GYROSCOPE){

//此处添加代码

}

}

if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR){

//此处添加代码

}

}

}

}

Mysensorlistener mylistener = new Mysensorlistener();

@Override

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

sm_gyroscope.unregisterListener(mylistener);

}

@Override

protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

sm_gyroscope.registerListener(mylistener, my_gyroscope, SensorManager.SENSOR_DELAY_NORMAL);

sm_gyroscope.registerListener(mylistener, my_rotation, SensorManager.SENSOR_DELAY_NORMAL);

}

更多相关文章

  1. android调用js
  2. Android(安卓)Jetpack Components of Lifecycle 学习笔记
  3. Android截屏实现——亲测有效代码
  4. Eclipse环境下格式化Android的代码风格
  5. java第四次实验
  6. Android(安卓)Duplicate files copied in APK
  7. android 127.0.0.1/localhost connection refused 问题的
  8. 从头开始学Android—Android(安卓)Studio(二)
  9. android实现图片平铺效果&WebView多点触控实现缩放

随机推荐

  1. Android关于buildToolVersion与CompileSd
  2. 分析通话记录信息是通过什么写入的 andro
  3. [置顶] Android(安卓) ExpandableListVi
  4. Android(安卓)Studio常用设置(持续更新..
  5. LeakCanary
  6. Kotlen_DataBinding_配置
  7. Android(安卓)LBS地图开发:地球地理GPS坐
  8. 如何改变Android(安卓)Progressbar默认颜
  9. Android游戏开发之旅七 自定义SurfaceVie
  10. Android导入工程出现 Project has no def