启动地图活动抛出空指针异常

时间:2013-08-05 08:51:12

标签: android

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

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

这是我的logcat

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;
    }
}

}

这是我的checkinactivity

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;
}

}

2 个答案:

答案 0 :(得分:0)

问题是因为您的Point为null。您应该验证该点是否与null不同,因为当您执行OverlayItem overlayitem = new OverlayItem时(点,“Hello World!”,             “我在这!”);该点为null,onCreate在onLocationChanged之前被调用。

对此评论感到抱歉,这是我在这里的第一天。

答案 1 :(得分:0)

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

CheckInActivity位于中间某处的onCreate(...)中,您可以创建一个OverlayItem对象:

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

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

我的猜测,请通过适当的调试来检查。