我使用地图,当我尝试使用EditText时,应用程序崩溃我使用了setContentView(R.layout.activity_maps);
但是获得了新的错误
当我用
删除行时 markerlocationtext.setText("Lat : " + CenterLOcation.latitude + "," + "Long : " + CenterLOcation.longitude);
应用程序运行良好
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap mGoogleMap;
Marker marker;
TextView markerlocationtext;
LatLng CenterLOcation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
markerlocationtext = (TextView)findViewById(R.id.locationMarkertext);
SaveSettings sv = new SaveSettings(this);
sv.LoadData();
final LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (googleServicesAvailabile()) {
setContentView(R.layout.activity_maps);
iniMap();
}
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}
}
private void iniMap() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public Boolean googleServicesAvailabile() {
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int isAvailable = api.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (api.isUserResolvableError(isAvailable)) {
Dialog dialog = api.getErrorDialog(this, isAvailable, 0);
dialog.show();
} else {
Toast.makeText(this, "Can't Connect to google play Services", Toast.LENGTH_LONG).show();
}
return false;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
Location UserLocation= getlocation();
new Location("phone");
if(UserLocation== null)
{
Toast.makeText(this,getResources().getString(R.string.gpsoff),Toast.LENGTH_LONG).show();
return;
}
UserLocation= getlocation();
gotolocationZoom(UserLocation.getLatitude(), UserLocation.getLongitude(), 15);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
{
return;
}
mGoogleMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
@Override
public void onCameraMoveStarted(int i)
{
}
});
mGoogleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
@Override
public void onCameraMove() {
}
});
mGoogleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
CenterLOcation = mGoogleMap.getCameraPosition().target;
}
});
markerlocationtext.setText("Lat : " + CenterLOcation.latitude + "," + "Long : " + CenterLOcation.longitude);
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getResources().getString(R.string.GpsDistable))
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
final AlertDialog alert = builder.create();
alert.show();
}
Location getlocation() {
Location myLocation = null;
try {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager != null) {
myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (myLocation == null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
}
}
catch (Exception ignored){
}
return myLocation;
}
private void gotolocationZoom(double lat, double lng, float zoom) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
mGoogleMap.animateCamera(update);
}
public void geoLocate(View view) throws IOException {
EditText editSearch = (EditText) findViewById(R.id.search);
String LocationText = editSearch.getText().toString();
if (LocationText.trim().length() > 0)
{
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(LocationText, 1);
Address address = list.get(0);
double lat = address.getLatitude();
double lng = address.getLongitude();
gotolocationZoom(lat, lng, 15);
}
}
private void setMarker(double lat, double lng) {
if (marker!= null)
{
marker.remove();
}
MarkerOptions options1 = new MarkerOptions()
.title("Here")
.position(new LatLng(lat,lng));
marker = mGoogleMap.addMarker(options1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapTypeNormal:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.mapTypeSatellite:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.mapTypeHyprid:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
错误显示我在没有SetContentView
的情况下使用Process: com.project.ytubasi.speedtaxi, PID: 6352
java.lang.NullPointerException
at com.project.ytubasi.speedtaxi.MapsActivity.onMapReady(MapsActivity.java:155)
at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:361)
at aai.a(:com.google.android.gms.DynamiteModulesB:82)
at maps.ad.t$5.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:855)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)
at dalvik.system.NativeStart.main(Native Method)
我在// setContentView(R.layout.activity_maps);
Process: com.project.ytubasi.speedtaxi, PID: 10899
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.ytubasi.speedtaxi/com.project.ytubasi.speedtaxi.MapsActivity}: android.view.InflateException: Binary XML file line #32: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:855)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at com.project.ytubasi.speedtaxi.MapsActivity.onCreate(MapsActivity.java:56)
at android.app.Activity.performCreate(Activity.java:5287)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:855)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #32: Duplicate id 0x7f0c0082, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2427)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:77)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at com.project.ytubasi.speedtaxi.MapsActivity.onCreate(MapsActivity.java:56)
at android.app.Activity.performCreate(Activity.java:5287)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:855)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)
at dalvik.system.NativeStart.main(Native Method)
XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:id="@+id/linearLayout2"
android:weightSum="2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button5"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button6"
android:layout_weight="1" />
</LinearLayout>
<fragment
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.project.ytubasi.speedtaxi.activity_maps.xml"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/search"
android:layout_below="@+id/linearLayout2"
/><LinearLayout
android:id="@+id/locationMarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical|center|center_horizontal"
android:orientation="vertical"
android:layout_alignTop="@+id/map"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp">
<TextView
android:id="@+id/locationMarkertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner_map"
android:gravity="center"
android:minWidth="250dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text=" Set your Location "
android:textColor="@android:color/white" />
<ImageView
android:id="@+id/imageMarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add_marker" />
</LinearLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/search"
android:layout_above="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/button4"
android:layout_toLeftOf="@+id/button4"
android:hint="@string/where_you_want_to_go"
android:text="@string/Search" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/linearLayout"
android:weightSum="2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button3"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_weight="1" />
</LinearLayout>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Find"
android:id="@+id/button4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/map"
android:layout_alignTop="@+id/search"
android:layout_alignBaseline="@+id/search"
android:layout_above="@+id/linearLayout"
android:onClick="geoLocate" />
</RelativeLayout>
答案 0 :(得分:1)
必须在super.onCreate
之后立即调用活动的布局文件super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
如果没有,您之前声明的组件(例如TextView
)就无法绑定到视图中。这就是为什么如果你没有评论这一行,你会得到NullPointerException
markerlocationtext.setText("Lat : " + CenterLOcation.latitude + "," + "Long : " + CenterLOcation.longitude);