我有一个EditText,其中输入了位置,当点击按钮时,将捆绑并通过意图将其发送到Details Activity,它接收位置名称并使用Geocoder获取经度和经度。但是当我尝试显然它在地图上显示我得到一个RunTimeException.Below是代码。这是获取一个地方的纬度和经度的正确方法。请帮助我。我是谷歌地图的新手。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView textView;
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("MainActivity:","Started");
List l = new ArrayList();
l.add("New Delhi");
l.add("Mumbai");
l.add("Chennai");
l.add("Bangalore");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Destination");
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,l);
textView.setAdapter(arrayAdapter);
final Bundle bundle =new Bundle();
bundle.putString("places",textView.getText().toString());
Button button1 = (Button) findViewById(R.id.enter);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Details.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}}
Details.java :(地图未包含,地址列表中的错误从地理编码器获取位置详细信息)
public class Details extends AppCompatActivity {
SupportMapFragment mapFragment;LatLng latLng;ArrayList<LatLng> markerPoints;LatLng from,to;Polyline polyline;ArrayList<Polyline> polylines;
private GoogleMap map;LocationManager manager;Double lat,longt;Location location;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
List<LatLng> latLngslist;
Marker marker;int i;String cityName,destination;
int start; ArrayList<LatLng> wapt;Routing routing;
Geocoder geocoder;int counter=0;
List<Address> wayptdetails;
String destinationname;
double destinationLatitude;
double destinationLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
mapFragment.getMapAsync(this);
getLocation();
wapt=new ArrayList<LatLng>();
wayptdetails=new ArrayList<Address>();
Bundle bundle= getIntent().getExtras();
destination= bundle.getString("place");
Geocoder geocoder1=new Geocoder(this,Locale.getDefault());
try {
List<Address> addressList=geocoder1.getFromLocationName(destination,1);
destinationLatitude= addressList.get(0).getLatitude();
destinationLongitude=addressList.get(0).getLongitude();
Log.e("Destination","Location retreived");
} catch (IOException e) {
e.printStackTrace();
}
}}
错误:
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: FATAL EXCEPTION: main
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobinsabu.destination/com.example.jobinsabu.destination.Details}: java.lang.IllegalArgumentException: locationName == null
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:153)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.os.Looper.loop(Looper.java:176)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5302)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: locationName == null
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.location.Geocoder.getFromLocationName(Geocoder.java:171)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at com.example.jobinsabu.destination.Details.onCreate(Details.java:78)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5326)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:153)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.os.Looper.loop(Looper.java:176)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5302)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-12 11:39:11.082 399-399/com.example.jobinsabu.destination E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
错误(编者):
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: FATAL EXCEPTION: main
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobinsabu.destination/com.example.jobinsabu.destination.Details}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:153)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.os.Looper.loop(Looper.java:176)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5302)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at java.util.ArrayList.get(ArrayList.java:304)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at com.example.jobinsabu.destination.Details.onCreate(Details.java:79)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5326)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:153)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.os.Looper.loop(Looper.java:176)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5302)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-12 12:00:20.371 4652-4652/com.example.jobinsabu.destination E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
与日志一样:
java.lang.IllegalArgumentException:locationName == null
因为来自destination
的{{1}}字符串为空。
在Bundle中添加getIntent()
键,点击按钮时places
值为:
textView.getText()
同样使用相同的密钥获取相同的密钥,以获取之前活动中使用的 bundle.putString("places",textView.getText().toString());
intent.putExtras(bundle);
活动中的值。
答案 1 :(得分:0)
在您的代码目的地为空,因为您通过了地方。
bundle.putString("places",textView.getText().toString());
但收到了
destination= bundle.getString("place");
//change into places.
像
一样改变 destination= bundle.getString("places");
在传递字符串之前检查textView.getText()。toString()不为空。