MainActivity:
public class MainActivity extends Activity implements SensorEventListener, OnClickListener {
private SensorManager mSensorManager;
private Sensor mAcc;
TextView xaxis,yaxis,zaxis,ballxy,gps,statics;
public float velocity = 10.6f;
public float xPosition,yPosition,zPosition;
public static float staticXposition,staticYposition,staticZposition;
private Database myDBAdapter;
public ContentValues values;
public String GPSposition;
public SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAcc = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
LocationManager locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locList = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
gps.setText("GPS:"+location.getLatitude()+";"+location.getLongitude());
GPSposition = location.getLatitude() + " " + location.getLongitude();
}
};
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locList);
// inicjowanie TextView
gps = (TextView) findViewById(R.id.gps);
xaxis = (TextView) findViewById(R.id.xaxis);
yaxis = (TextView) findViewById(R.id.yaxis);
zaxis = (TextView) findViewById(R.id.zaxis);
ballxy = (TextView) findViewById(R.id.ballxy);
statics = (TextView) findViewById(R.id.statics);
Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(this);
myDBAdapter = new Database(this).open();
//TODO Any database operations
myDBAdapter.close();
}
public void insertData(){
String time;
Time now = new Time();
now.setToNow();
time = now.toString();
values.put("ID", "1");
values.put("LEFT_POSITION", xPosition);
values.put("RIGHT_POSITION", yPosition);
values.put("GPS", GPSposition);
values.put("TIME", time);
if(db!=null){
db.insert("baza", null, values);
}else{
Log.d("AppName","db is null");
}
}
@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 void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public final void onSensorChanged(SensorEvent event) {
ImageView ball = (ImageView) findViewById(R.id.ball);
xPosition = event.values[0] - staticXposition;
yPosition = event.values[1] - staticYposition;
zPosition = event.values[2] - staticZposition;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mSensorManager.registerListener(this, mAcc, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.start:
staticXposition = xPosition;
staticYposition = yPosition;
staticZposition = zPosition;
insertData();
}
}
}
我用contentValues创建了方法:
public void insertData(){
String time;
Time now = new Time();
now.setToNow();
time = now.toString();
values.put("ID", "2");
values.put("LEFT_POSITION", xPosition);
values.put("RIGHT_POSITION", xPosition);
values.put("GPS", GPSposition);
values.put("TIME", time);
db.insert("baza", null, values);
}
当用户按下按钮时,我正在尝试将数据发送到数据库“baza”。
public void onClick(View v) {
switch(v.getId()){
case R.id.start:
staticXposition = xPosition;
staticYposition = yPosition;
staticZposition = zPosition;
insertData();
}
}
我有一些变量:xPosition,yPosition和zPosition。此变量中的数据是动态的,取决于加速度计。我正在尝试将此数据发送到我的数据库,但我有一个错误:致命异常主要。空指针异常。哪里有错误?
01-12 12:42:17.093: E/AndroidRuntime(9216): FATAL EXCEPTION: main
01-12 12:42:17.093: E/AndroidRuntime(9216): java.lang.NullPointerException
01-12 12:42:17.093: E/AndroidRuntime(9216): at pl.pawelfrydrych.flyingball.MainActivity.insertData(MainActivity.java:123)
01-12 12:42:17.093: E/AndroidRuntime(9216): at pl.pawelfrydrych.flyingball.MainActivity.onClick(MainActivity.java:218)
01-12 12:42:17.093: E/AndroidRuntime(9216): at android.view.View.performClick(View.java:4240)
01-12 12:42:17.093: E/AndroidRuntime(9216): at android.view.View$PerformClick.run(View.java:17721)
01-12 12:42:17.093: E/AndroidRuntime(9216): at android.os.Handler.handleCallback(Handler.java:730)
01-12 12:42:17.093: E/AndroidRuntime(9216): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 12:42:17.093: E/AndroidRuntime(9216): at android.os.Looper.loop(Looper.java:137)
01-12 12:42:17.093: E/AndroidRuntime(9216): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-12 12:42:17.093: E/AndroidRuntime(9216): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 12:42:17.093: E/AndroidRuntime(9216): at java.lang.reflect.Method.invoke(Method.java:525)
01-12 12:42:17.093: E/AndroidRuntime(9216): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-12 12:42:17.093: E/AndroidRuntime(9216): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-12 12:42:17.093: E/AndroidRuntime(9216): at dalvik.system.NativeStart.main(Native Method)
01-12 12:42:19.673: I/Process(9216): Sending signal. PID: 9216 SIG: 9
答案 0 :(得分:0)
检查您的数据库 试试这段代码:
public void insertData(){
String time;
Time now = new Time();
now.setToNow();
time = now.toString();
values.put("ID", "2");
values.put("LEFT_POSITION", xPosition);
values.put("RIGHT_POSITION", xPosition);
values.put("GPS", GPSposition);
values.put("TIME", time);
if(db!=null)
db.insert("baza", null, values);
else
Log.d("AppName","db is null);
}
编辑答案:
myDBAdapter = new Database(this).open();
//TODO Any database operations
myDBAdapter.close();
为什么要关闭数据库适配器?插入值后,应该关闭.Plus在哪里初始化db?
编辑没有。 2: 在您的代码中更改此内容
protected void onStop() {
myDBAdapter.close();;
}
活动的可见生命周期发生在对onStart()的调用之间,直到对onStop()的相应调用。在此期间,用户可以在屏幕上看到活动,尽管它可能不在前台并与用户交互。在这两种方法之间,您可以维护向用户显示活动所需的资源。例如,您可以在onStart()中注册BroadcastReceiver以监视影响UI的更改,并在用户不再看到您正在显示的内容时在onStop()中取消注册。 onStart()和onStop()方法可以多次调用,因为活动对用户来说是可见的和隐藏的。
请浏览android的基础知识。