簡體   English   中英

為什么我的String初始化拋出NullPointerError?

[英]Why is my String initialization throwing a NullPointerError?

我一直在Android Studio中編寫一個android應用程序,目的是從Google Directions API中獲取JSONObject(使用Volley庫),然后將其傳遞給新的活動(然后在其中進行打印,這樣我就可以在解析之前進行調試)。 但是,通過ADB在手機上運行應用程序后, LogCat會返回this

提交按鈕的onClick調用的方法(在應用崩潰時)為:

public void submit(View view) {
    Location location = LocationServices.FusedLocationApi.getLastLocation(locationClient);
    String destination = findViewById(R.id.destinationInput).toString();
    String url = "https://maps.googleapis.com/maps/api/directions/json?origin="
            + location.getLatitude()
            + location.getLongitude()
            + "&destination=" + destination
            + "&mode=driving"
            + avoids()
            + "departure_time=now"
            + "&key=" + getString(R.string.API_KEY);

    RequestQueue requestQueue;
    // Instantiate the cache
    Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
    // Set up the network to use HttpURLConnection as the HTTP client.
    Network network = new BasicNetwork(new HurlStack());
    // Instantiate the RequestQueue with the cache and network.
    requestQueue = new RequestQueue(cache, network);
    // Start the queue
    requestQueue.start();

    JsonObjectRequest jsObjRequest = new JsonObjectRequest
            (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject dirs) {
                    Intent intent;
                    intent = new Intent(getApplicationContext(), DisplayDirections.class);
                    intent.putExtra("DIRECTIONS", dirs.toString());
                    startActivity(intent);
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    // Add the request to the RequestQueue.
    requestQueue.add(jsObjRequest);
}

private String avoids() {
    String avoid = "avoid=";
    boolean any = false;
    if(((Switch) findViewById(R.id.avoidTollsSwitch)).isChecked()) {
        avoid += "tolls";
        any = true;
    }
    if(((Switch) findViewById(R.id.avoidMotorwaysSwitch)).isChecked()) {
        if(any) avoid += "|";
        avoid += "highways";
        any = true;
    }
    if(((Switch) findViewById(R.id.avoidFerriesSwitch)).isChecked()) {
        if(any) avoid += "|";
        avoid += "ferries";
        any = true;
    }
    if(any) return avoid;
    else return "";
}

LogCat日志鏈接到String url =行,但是我對這行的錯誤感到困惑。 據我所知,我的代碼應該可以工作...

排球教程: https : //developer.android.com/training/volley/simple.html

您確定location不為空嗎? String url =行上放置一個斷點,看看它的值是多少。

如果在模擬器上運行它,則需要將坐標發送到模擬器。 否則, location將為null。

第一次使用地理位置服務時,我的頭發就拉扯了一下。

像這樣檢查一下

if (location!=null) {
foo.doSomethingWithLocation(bar);
} else {
 Log.d(TAG, "Sorry. Location is null");
}

在你打電話之前

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM