我是Android的新手,正在尝试在我的项目中使用Google地图。我的活动在ADT捆绑包的模拟器上完美运行。但是它真的很慢。当我在Genymotion上运行它时,它给出了nullpointer异常。
@SuppressLint("NewApi")
public class EditPlace extends Activity
implements LocationListener
{
private boolean isEditMode=true;
private GoogleMap gmap;
final List<String> Rlist = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*final Spinner radius = (Spinner) findViewById(R.id.spinner);*/
setContentView(R.layout.activity_edit_place);
populate();
final Serializable extra = getIntent().getSerializableExtra("Place");
final EditText pName = (EditText)findViewById(R.id.editText1);
gmap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
gmap.setMyLocationEnabled(true);
gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gmap.setOnMapLongClickListener(onLongClickMapSettins());
final Button saveButton = (Button)findViewById(R.id.button2);
Myplace Place = (Myplace)extra;
if(extra!=null)
{
isEditMode=false;
pName.setText(Place.getTitle());
radius.setSelection(Place.getIdLocation().getRadius());
final LatLng curPlace = new LatLng(Place.getIdLocation().getX(), Place.getIdLocation().getY());
marker = gmap.addMarker(new MarkerOptions().position(curPlace));
CameraUpdate camup = CameraUpdateFactory.newLatLngZoom(curPlace, 16);
gmap.animateCamera(camup);
pName.setEnabled(false);
radius.setEnabled(false);
gmap.setOnMapLongClickListener(null);
saveButton.setText("Edit");
}
saveButton.setOnClickListener(new OnClickListener(){
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
if(isEditMode==false)
{
gmap.setOnMapLongClickListener(onLongClickMapSettins());
pName.setEnabled(true);
radius.setEnabled(true);
isEditMode=true;
if(marker!=null) marker.remove();
marker=null;
saveButton.setText("Save");
}
else
{
if(marker == null) {
warnMzg();
return;}
else{
/*System.out.println(name.getText().toString());*/
double lat = marker.getPosition().latitude;
double lng = marker.getPosition().longitude;
/*System.out.println(lat);
System.out.println(lng);
System.out.println(radius.getSelectedItem());*/
int Radius = radius.getSelectedItemPosition();
location loc = new location();
loc.setX(lat);
loc.setY(lng);
loc.setRadius(Radius);
Myplace newPlace = new Myplace();
newPlace.setTitle(pName.getText().toString());
newPlace.setIdLocation(loc);
System.out.println(newPlace.getTitle());
System.out.println(newPlace.getIdLocation().getX());
System.out.println(newPlace.getIdLocation().getY());
System.out.println(newPlace.getIdLocation().getRadius());}
// TODO Auto-generated method stub
}}
});}
/*gmap.addMarker(new MarkerOptions().position(ucsc));*/
/*gmap.setOnMapClickListener((OnMapClickListener) this);*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.edit_place, menu);
return true;
}
/*public void onCilck_clear(View view){
CameraUpdate camup = CameraUpdateFactory.newLatLngZoom(ucsc, 16);
gmap.animateCamera(camup);
}*/
private void warnMzg() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("No Location Selected");
alert.setMessage("Please hold and select a point on the map");
alert.setPositiveButton("OK",null);
alert.show();
}
public static Spinner radius;
public void populate() {
radius = (Spinner) findViewById(R.id.spin1);
Rlist.add("50");
Rlist.add("100");
Rlist.add("200");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, Rlist);
radius.setAdapter(adapter);
}
public static Marker marker=null;
public OnMapLongClickListener onLongClickMapSettins(){
return new OnMapLongClickListener(){
@Override
public void onMapLongClick(LatLng pos) {
// TODO Auto-generated method stub
if(marker!=null) marker.remove();
marker=null;
marker = gmap.addMarker(new MarkerOptions().position(pos));
Log.i(pos.toString(), "User Long Clicked");
}
};
}
这是布局
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/editText1" />
<Button
android:id="@+id/button2"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="onClick_save"
android:text="@string/save" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="@string/Name"
android:layout_above="@+id/spin1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:text="@string/radius" />
<Spinner
android:id="@+id/spin1"
android:layout_width="220dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_above="@+id/button2" />
这是logcat给出的错误:
10-09 14:03:09.013: E/AndroidRuntime(1727): FATAL EXCEPTION: main
10-09 14:03:09.013: E/AndroidRuntime(1727): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ucsc.loctip/com.ucsc.loctip.EditPlace}: java.lang.NullPointerException
10-09 14:03:09.013: E/AndroidRuntime(1727): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-09 14:03:09.013: E/AndroidRuntime(1727): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-09 14:03:09.013: E/AndroidRuntime(1727): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-09 14:03:09.013: E/AndroidRuntime(1727): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-09 14:03:09.013: E/AndroidRuntime(1727): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 14:03:09.013: E/AndroidRuntime(1727): at android.os.Looper.loop(Looper.java:137)
10-09 14:03:09.013: E/AndroidRuntime(1727): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-09 14:03:09.013: E/AndroidRuntime(1727): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 14:03:09.013: E/AndroidRuntime(1727): at java.lang.reflect.Method.invoke(Method.java:511)
10-09 14:03:09.013: E/AndroidRuntime(1727): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
最佳答案
在使用Play服务之前,您似乎必须将虚拟设备与Google帐户关联。
关于android - 在Genymotion上运行时,谷歌 map 给出了NullPointer异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19279081/