Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当没有持续的internet连接时,应用程序崩溃_Java_Android_Google Maps_Android Studio - Fatal编程技术网

Java 当没有持续的internet连接时,应用程序崩溃

Java 当没有持续的internet连接时,应用程序崩溃,java,android,google-maps,android-studio,Java,Android,Google Maps,Android Studio,以下代码来自Google maps活动,用于在用户输入中查找特定位置。它工作正常,但在没有持续的internet连接时崩溃。请帮助我解决此问题。 MainActivity.java类如下所示: import android.location.Address; import android.location.Geocoder; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.F

以下代码来自Google maps活动,用于在用户输入中查找特定位置。它工作正常,但在没有持续的internet连接时崩溃。请帮助我解决此问题。 MainActivity.java类如下所示:

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));
                }
            }
        }
    }
}

在没有internet连接的情况下,
地址
。因此,在显示
Toast
消息后,您应该
返回控件

if(addresses==null || addresses.size()==0){
        Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
        return;
}

在网络呼叫前检查互联网任何故障日志都可以帮助我们快速识别您的问题这让您感到惊讶吗?应用程序需要互联网,应用程序没有互联网,应用程序尝试使用互联网做一些事情,应用程序不处理无互联网->崩溃如果没有看到崩溃报告,您将无法在此处获得解决方案,请发布日志输出…好的。现在它停止崩溃。但有时我会收到“找不到位置”即使有互联网,也会发送toast消息…这意味着他们有时不会读取输入的字符串。这是因为我使用2G互联网连接还是因为代码有问题?顺便说一句。谢谢你帮助我阻止我的应用程序崩溃!“找不到任何位置”是完全不同的情况。这意味着地理编码器无法找到给定输入的任何地址。好的,我理解。但当我输入“纽约”时,有时它指向纽约……但有时它显示“找不到位置”(以前它崩溃,但现在它是“找不到位置”)为什么会这样呢?嗨,Aravind,如果这个或任何答案都解决了你的问题,请点击一下复选标记。这向更广泛的社区表明,你已经找到了解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。
if(addresses==null || addresses.size()==0){
        Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
        return;
}