現在の 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;
}
}
}
これは私のチェックインアクティビティです
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;
}
}