0

我正在尝试启动地图活动,以获取您当前的 gps 位置。按下按钮时它工作正常,直到我决定为用户添加一项功能,以便在禁用时打开他的 gps。我使用了我在 Google 上找到的一个代码片段,它使用了某种处理程序类。它没有用,我一直在我的错误日志中看到它,所以我把它拿出来了。但是每当我按下按钮时,程序都会继续返回空指针异常。

我的问题是“我如何让程序像以前一样重新开始地图活动,如果可能的话,我如何进行 gps 检查

这是我的日志猫

08-05 08:24:50.411: E/AndroidRuntime(17838): FATAL EXCEPTION: main
08-05 08:24:50.411: E/AndroidRuntime(17838): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.g6.georeminderv3/com.g6.georeminderv3.CheckInActivity}: java.lang.NullPointerException
08-05 08:24:50.411: E/AndroidRuntime(17838):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at android.os.Looper.loop(Looper.java:150)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at android.app.ActivityThread.main(ActivityThread.java:4385)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at java.lang.reflect.Method.invokeNative(Native Method)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at java.lang.reflect.Method.invoke(Method.java:507)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at dalvik.system.NativeStart.main(Native Method)
08-05 08:24:50.411: E/AndroidRuntime(17838): Caused by: java.lang.NullPointerException
08-05 08:24:50.411: E/AndroidRuntime(17838):    at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at com.g6.georeminderv3.OverlayItems.addOverlay(OverlayItems.java:22)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at com.g6.georeminderv3.CheckInActivity.onCreate(CheckInActivity.java:100)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
08-05 08:24:50.411: E/AndroidRuntime(17838):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
08-05 08:24:50.411: E/AndroidRuntime(17838):    ... 11 more
08-05 08:24:52.322: D/Process(17838): killProcess, pid=17838
08-05 08:24:52.322: D/Process(17838): dalvik.system.VMStack.getThreadStackTrace(Native Method)
08-05 08:24:52.322: D/Process(17838): java.lang.Thread.getStackTrace(Thread.java:745)
08-05 08:24:52.322: D/Process(17838): android.os.Process.killProcess(Process.java:797)
08-05 08:24:52.322: D/Process(17838): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
08-05 08:24:52.322: D/Process(17838): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:854)
08-05 08:24:52.322: D/Process(17838): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:851)
08-05 08:24:52.322: D/Process(17838): dalvik.system.NativeStart.main(Native Method)
08-05 08:24:52.322: I/Process(17838): Sending signal. PID: 17838 SIG: 9

这是我开始签入活动的主要活动

public class MainActivity extends Activity implements OnClickListener {
ImageButton add, settings, checkIn;
ListView list;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    findviews();
    populateListView();

    settings.setOnClickListener(this);
    add.setOnClickListener(this);
    checkIn.setOnClickListener(this);

}

private void populateListView() {
    DatabaseHandler db = new DatabaseHandler(this);



    // start here
    SQLiteDatabase database = db.getReadableDatabase();
    Log.d(DatabaseHandler.TAG, DatabaseHandler.actionsSelectQuery);
    String[] columns = new String[] { DatabaseHandler.KEY_ACTIONS_ID,
            DatabaseHandler.KEY_ACTIONS_TITLE,
            DatabaseHandler.KEY_ACTIONS_PLACE_NAME,
            DatabaseHandler.KEY_ACTIONS_TIME,
            DatabaseHandler.KEY_ACTIONS_DATE };

    // Cursor cursor = database.rawQuery(db.actionsSelectQuery, null);
    Cursor cursor = database.query(DatabaseHandler.TABLE_ACTIONS, columns,
            null, null, null, null, DatabaseHandler.KEY_ACTIONS_ID + " DESC");

    String[] from = new String[] { DatabaseHandler.KEY_ACTIONS_TITLE,
            DatabaseHandler.KEY_ACTIONS_PLACE_NAME,
            DatabaseHandler.KEY_ACTIONS_TIME,
            DatabaseHandler.KEY_ACTIONS_DATE };
    int[] to = { R.id.title_text, R.id.place_text, R.id.time_text,
            R.id.date_text };

    @SuppressWarnings("deprecation")
    SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(
            MainActivity.this, R.layout.action_list_layout, cursor, from,
            to);
    list.setAdapter(listAdapter);
    Log.d(DatabaseHandler.TAG, "list view created");
    // mySQLiteAdapter.close();

}

private void findviews() {
    settings = (ImageButton) findViewById(R.id.settings);
    add = (ImageButton) findViewById(R.id.add_reminder);
    checkIn = (ImageButton) findViewById(R.id.check_in);

    list = (ListView) findViewById(R.id.action_list_view);

}

@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);
    //add fav manager here
    return true;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.settings:
        startActivity(new Intent(MainActivity.this, SettingsActivity.class));
        break;
    case R.id.add_reminder:
        startActivity(new Intent(MainActivity.this, AddReminder.class));
        MainActivity.this.finish();
        break;
    case R.id.check_in:
        startActivity(new Intent(MainActivity.this, CheckInActivity.class));
        break;
    }
}

}

这是我的检查活动

public class CheckInActivity extends MapActivity {
private int place_longitude;
private int place_latitude;
Button save;
EditText chkInPlaceName;
String chkSavePlaceName,setPlongString,setPlatString;
TextView setPlong,setPlat;
GeoPoint point;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_check_in);

    MapView mapView = (MapView) findViewById(R.id.mapview);
    final MapController controller = mapView.getController();

    LocationManager manager = (LocationManager) this
            .getSystemService(LOCATION_SERVICE);
    LocationListener listener = 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) {

        }

        @Override
        public void onLocationChanged(Location location) {
            setPlace_latitude((int) location.getLatitude());
            setPlace_longitude((int) location.getLongitude());

            point = new GeoPoint(getPlace_latitude(), getPlace_longitude());

            controller.setCenter(point);
        }
    };

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 900, 300,
            listener);
    // TODO 900 is the number of seconds to triger a location updat set it
    // up in the prefrence
    // TODO 300 is the minimum distace in metres to trigger a location up.
    // set it up in settings

    // pinin starts here
    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
    OverlayItems itemizedoverlay = new OverlayItems(drawable, this);

    setPlat = (TextView) findViewById(R.id.set_plat);
    setPlong = (TextView) findViewById(R.id.set_plong);
    setPlat.setText("Latitude: " + getPlace_latitude());
    setPlong.setText("Longitude: " + getPlace_longitude());


    OverlayItem overlayitem = new OverlayItem(point, "Hello World!",
            "I'm here!");


    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);

    save = (Button) findViewById(R.id.save_btn);
    save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            chkInPlaceName = (EditText) findViewById(R.id.check_in_place_name);
            chkSavePlaceName = chkInPlaceName.getText().toString();
            if (chkSavePlaceName.equals("")) {
                Toast.makeText(CheckInActivity.this, "Enter Place name",
                        Toast.LENGTH_SHORT).show();

            } else {
                DatabaseHandler db = new DatabaseHandler(
                        CheckInActivity.this);
                /**
                 * CRUD Operations
                 * */
                // Inserting Action
                db.addPlace(new Places(chkSavePlaceName,
                        getPlace_latitude(), getPlace_longitude()));
                startActivity(new Intent(CheckInActivity.this,
                        MainActivity.class));
                CheckInActivity.this.finish();

            }

        }
    });

    // pinning ends here
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.check_in, menu);
    return true;
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

public int getPlace_longitude() {
    return place_longitude;
}

public void setPlace_longitude(int place_longitude) {
    this.place_longitude = place_longitude;
}

public int getPlace_latitude() {
    return place_latitude;
}

public void setPlace_latitude(int place_latitude) {
    this.place_latitude = place_latitude;
}

}

这是覆盖类

public class OverlayItems extends ItemizedOverlay<OverlayItem>{

Context mContext;

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

public OverlayItems(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

@Override
protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
    }

@Override
public int size() {
    return mOverlays.size();
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = mOverlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

}

4

2 回答 2

0

问题是因为您的 Point 为空。您应该验证该点是否与 null 不同,因为当您执行 OverlayItem overlayitem = new OverlayItem(point, "Hello World!", "I'm here!"); 该点为空,在 onLocationChanged 之前调用 onCreate。

对不起,这是我在这里的第一天。

于 2013-08-05T09:09:50.093 回答
0

这是我的答案,但我只能猜测,因为我无法对您的代码进行完整的调试。

在中间CheckInActivity的某个地方创建一个对象:onCreate(...)OverlayItem

OverlayItem overlayitem = new OverlayItem(point, "Hello World!", "I'm here!");

参数是一个成员变量,a GeoPointLocationManager在您分配第一个值之前,此变量尚未初始化onLocationChanged()。由于 LocationManager 可能需要一段时间来获取位置并将其分配给 GeoPoint,因此您创建 OverlayItem 的行可能会在之前执行,因此point仍然是null.

这是我的猜测,请通过适当的调试进行检查。

于 2013-08-05T09:57:40.537 回答