在这个程序中,我有3个按钮和1个textVIEw.
在当前阶段,我基本上将很多事情留空了.
例如我尚未实现按钮功能.
但是现在里面有一个运行时错误.
甚至元素也无法显示出来.
能否请你帮忙?
import androID.harDWare.Sensor;import androID.harDWare.SensorEvent;import androID.harDWare.SensorEventListener;import androID.harDWare.SensorManager;import androID.os.Bundle;import androID.app.Activity;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.TextVIEw;public class MainActivity extends Activity implements SensorEventListener { button startbutton; button pausebutton; button resetbutton; TextVIEw textVIEw = (TextVIEw) findVIEwByID(R.ID.textVIEw1); private SensorManager sensorManager; private long lastUpdate; private boolean isCalibrated = false; private int referrenceAxis; private int stepCounts = 0; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // start button startbutton = (button) findVIEwByID(R.ID.button1); startbutton.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { } }); // pause button pausebutton = (button) findVIEwByID(R.ID.button2); pausebutton.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { } }); // reset button resetbutton = (button) findVIEwByID(R.ID.button3); resetbutton.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { } }); init(); } @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; } public voID init() { sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); lastUpdate = System.currentTimeMillis(); } public voID onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_liNEAR_acceleration) { long actualTime = System.currentTimeMillis(); if (isCalibrated && (actualTime - lastUpdate > 500)) { if (event.values[referrenceAxis] > 1) { stepCounts++; } } } if (event.sensor.getType() == Sensor.TYPE_GraviTY) { if (!isCalibrated) { if ((event.values[0] >= event.values[1])&&(event.values[0] >= event.values[2])) referrenceAxis = 0; if ((event.values[1] >= event.values[0])&&(event.values[1] >= event.values[2])) referrenceAxis = 1; if ((event.values[2] >= event.values[0])&&(event.values[2] >= event.values[1])) referrenceAxis = 2; isCalibrated = true; } } } public voID onAccuracyChanged(Sensor sensor, int accuracy) { } protected voID onResume() { super.onResume(); // register this class as a Listener for the sensors sensorManager .registerListener(this, sensorManager .getDefaultSensor(Sensor.TYPE_liNEAR_acceleration), SensorManager.SENSOR_DELAY_norMAL); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GraviTY), SensorManager.SENSOR_DELAY_norMAL); } protected voID onPause() { // unregister Listener super.onPause(); sensorManager.unregisterListener(this); }}
解决方法:
采用
TextVIEw textVIEw = (TextVIEw) findVIEwByID(R.ID.textVIEw1);
后
setContentVIEw(R.layout.activity_main);
膨胀父级布局之前,不能膨胀子视图.因此,在您的情况下,将textVIEw声明为成员变量,然后在setContentVIEw之后对其进行初始化.所以你的实现就像
作为成员变量声明
TextVIEw textVIEw;
在onCreate中
setContentVIEw(R.layout.activity_main);TextVIEw textVIEw = (TextVIEw) findVIEwByID(R.ID.textVIEw1);
总结 以上是内存溢出为你收集整理的Android-RuntimeException,哪里出问题了?全部内容,希望文章能够帮你解决Android-RuntimeException,哪里出问题了?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)