我创建了一个Android应用,将经纬度插入本地创建的数据库中。但是,当我单击位置按钮时,它在行46上给了我一些logcat错误。单击“位置”按钮时,它将纬度和经度传递给数据库。有人可以帮我解决这个问题吗?

46行是:

long result = dbm.insert("location",null,cv);


这是我的日志:

12-29 16:04:22.480: D/gralloc_goldfish(1319): Emulator without GPU emulation detected.
12-29 16:19:55.630: D/GPS Enabled(1319): GPS Enabled
12-29 16:19:55.650: D/AndroidRuntime(1319): Shutting down VM
12-29 16:19:55.650: W/dalvikvm(1319): threadid=1: thread exiting with uncaught exception (group=0xb39feba8)
12-29 16:19:55.700: E/AndroidRuntime(1319): FATAL EXCEPTION: main
12-29 16:19:55.700: E/AndroidRuntime(1319): Process: com.example.gpstracking, PID: 1319
12-29 16:19:55.700: E/AndroidRuntime(1319): java.lang.NullPointerException
12-29 16:19:55.700: E/AndroidRuntime(1319):     at com.example.gpstracking.AndroidGPSTrackingActivity$1.onClick(AndroidGPSTrackingActivity.java:46)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at android.view.View.performClick(View.java:4438)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at android.view.View$PerformClick.run(View.java:18422)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at android.os.Handler.handleCallback(Handler.java:733)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at android.os.Looper.loop(Looper.java:136)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at android.app.ActivityThread.main(ActivityThread.java:5001)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at java.lang.reflect.Method.invoke(Method.java:515)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-29 16:19:55.700: E/AndroidRuntime(1319):     at dalvik.system.NativeStart.main(Native Method)
12-29 16:19:59.060: I/Process(1319): Sending signal. PID: 1319 SIG: 9


这是我的主要Activity

package com.example.gpstracking;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidGPSTrackingActivity extends Activity {

    Button btnShowLocation;

    // GPSTracker class
    GPSTracker gps;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // create class object
                gps = new GPSTracker(AndroidGPSTrackingActivity.this);

                // check if GPS enabled
                if(gps.canGetLocation()){

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                    SQLiteDatabase dbm = this.getWritableDatabase();
                    ContentValues cv = new ContentValues();

                    cv.put("latitude", gps.getLatitude());
                    cv.put("longitude", gps.getLongitude());


                    long result = dbm.insert("location",null,cv);



                   Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                }else{
                    gps.showSettingsAlert();
                }

            }

            private SQLiteDatabase getWritableDatabase() {
                // TODO Auto-generated method stub
                return null;
            }
        });
    }
}

最佳答案

适当地实现此方法:

   private SQLiteDatabase getWritableDatabase() {
        // TODO Auto-generated method stub
        return null;
    }


此方法返回null

 SQLiteDatabase dbm = this.getWritableDatabase();


因此dbm为null,因此在第46行获得了null指针异常。

12-29 16:19:55.700: E/AndroidRuntime(1319):     at com.example.gpstracking.AndroidGPSTrackingActivity$1.onClick(AndroidGPSTrackingActivity.java:46)


点击以下链接了解如何在db中读取/写入

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

10-01 01:43
查看更多