应用程序崩溃时,有没有持续的互联网连接 [英] App Crashes when there is no persistent internet connection
本文介绍了应用程序崩溃时,有没有持续的互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下code是从谷歌地图的活动找到用户input.It工作正常,但崩溃的特定位置时,有没有持续的互联网connection.Please帮我解决这个问题。
该MainActivity.java类看起来是这样的:
进口android.location.Address;
进口android.location.Geo codeR;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
进口com.google.android.gms.maps.CameraUpdateFactory;
进口com.google.android.gms.maps.GoogleMap;
进口com.google.android.gms.maps.SupportMapFragment;
进口com.google.android.gms.maps.model.LatLng;
进口com.google.android.gms.maps.model.MarkerOptions;
进口java.io.IOException异常;
进口的java.util.List;公共类MainActivity
扩展FragmentActivity { GoogleMap的GoogleMap的;
的MarkerOptions的MarkerOptions;
经纬度的latLng; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
SupportMapFragment supportMapFragment =(SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。
//获取到地图的参考
GoogleMap的= supportMapFragment.getMap();
//获取参考btn_find布局activity_main的
按钮btn_find =(按钮)findViewById(R.id.btn_find);
//定义按钮点击事件侦听器查找按钮
OnClickListener findClickListener =新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
的EditText etLocation =(EditText上)findViewById(R.id.et_location);
//获取用户输入的位置
。字符串位置= etLocation.getText()的toString();
如果(位置= NULL&放大器;!&安培;!location.equals()){
新的地缘coderTask()执行(位置);
}
}
};
//设置按钮点击事件侦听器查找按钮
btn_find.setOnClickListener(findClickListener);
} //一个AsyncTask的类访问地址解析网络服务
私有类地理coderTask
延伸AsyncTask的<弦乐,太虚,列表与LT;地址>> { @覆盖
保护列表与LT;地址> doInBackground(字符串... LOCATIONNAME){
//创建地理codeR类的一个实例
地理codeR地理codeR =新的地缘codeR(getBaseContext());
清单<地址>地址= NULL;
尝试{
//获取最多3个地址,输入文本相匹配
地址=地理coder.getFromLocationName(LOCATIONNAME [0],3);
}赶上(IOException异常五){
e.printStackTrace();
}
返回地址;
} @覆盖
保护无效onPostExecute(列表<地址>地址){
如果(地址== NULL || addresses.size()== 0){
Toast.makeText(getBaseContext(),无位置找到Toast.LENGTH_SHORT).show();
}
//清除地图上所有的现有标记
googleMap.clear();
//添加谷歌地图上标记为每个匹配的地址
的for(int i = 0; I< addresses.size();我++){
地址地址=(地址)addresses.get(I)
//创建的GeoPoint的一个实例,在谷歌地图中显示
的latLng =新经纬度(address.getLatitude(),address.getLongitude());
串addressText =的String.format(%S%S,address.getMaxAddressLineIndex()大于0 address.getAddressLine(0):,address.getCountryName());
的MarkerOptions =新的MarkerOptions();
markerOptions.position(的latLng);
markerOptions.title(addressText);
googleMap.addMarker(的MarkerOptions);
//找到的第一个位置
如果(我== 0){
googleMap.animateCamera(CameraUpdateFactory.newLatLng(的latLng));
}
}
}
}
}
这是我的崩溃报告:
02-25 17:17:42.157 4312-4312 / aravindaby.myapp123 E /测试:异常
02-25 17:17:42.157 4312-4312 / aravindaby.myapp123 E / AndroidRuntime:致命异常:主要
显示java.lang.NullPointerException
在aravindaby.myapp123.MainActivity $地理coderTask.onPostExecute(MainActivity.java:100)
在aravindaby.myapp123.MainActivity $地理coderTask.onPostExecute(MainActivity.java:72)
在android.os.AsyncTask.finish(AsyncTask.java:631)
在android.os.AsyncTask.access $ 600(AsyncTask.java:177)
在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4793)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:808)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
在dalvik.system.NativeStart.main(本机方法)
解决方案
在没有互联网连接的情况下,地址
是空
。所以显示吐司
消息后,你应该收益
控制。
如果(地址== NULL || addresses.size()== 0){
Toast.makeText(getBaseContext(),无位置找到Toast.LENGTH_SHORT).show();
返回;
}
The following code is from a Google maps activity to find a particular location on user input.It works fine but crashes when there is no persistent internet connection.Please help me fix this problem. The MainActivity.java class looks like this:
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
public class MainActivity
extends FragmentActivity {
GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting a reference to the map
googleMap = supportMapFragment.getMap();
// Getting reference to btn_find of the layout activity_main
Button btn_find = (Button) findViewById(R.id.btn_find);
// Defining button click event listener for the find button
OnClickListener findClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText etLocation = (EditText) findViewById(R.id.et_location);
// Getting user input location
String location = etLocation.getText().toString();
if (location != null && !location.equals("")) {
new GeocoderTask().execute(location);
}
}
};
// Setting button click event listener for the find button
btn_find.setOnClickListener(findClickListener);
}
// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask
extends AsyncTask<String, Void, List<Address>> {
@Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if (addresses == null || addresses.size() == 0) {
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
googleMap.clear();
// Adding Markers on Google Map for each matching address
for (int i = 0; i < addresses.size(); i++) {
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
googleMap.addMarker(markerOptions);
// Locate the first location
if (i == 0) {
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
}
}
And this is my crash report:
02-25 17:17:42.157 4312-4312/aravindaby.myapp123 E/test: Exception
02-25 17:17:42.157 4312-4312/aravindaby.myapp123 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at aravindaby.myapp123.MainActivity$GeocoderTask.onPostExecute(MainActivity.java:100)
at aravindaby.myapp123.MainActivity$GeocoderTask.onPostExecute(MainActivity.java:72)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4793)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)
解决方案
In the case of no internet connection, address
is null
. So after showing Toast
message, you should return
the control.
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
return;
}
这篇关于应用程序崩溃时,有没有持续的互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文