【问题标题】:NullPointerException in AsyncTask OnPostExecute with google maps带有谷歌地图的 AsyncTask OnPostExecute 中的 NullPointerException
【发布时间】:2018-12-09 23:11:35
【问题描述】:

When debugging I get this

在'httpURLConnection.connect();'行之后我的代码转到 catch 块并给我这个输出:

output catch part 2

我知道有几个关于这个主题的帖子,但我似乎没有找到解决方案。我不明白为什么我会收到这个错误。我知道我很可能从我的“doInbackground()”方法中得到一个空对象,但我不明白为什么。有人可以帮助我理解这一点,以便我知道如何解决它。谢谢 !

我的代码:

    private  class PlaceRequest extends AsyncTask<String, Integer, JSONArray> {

        @Override
        protected JSONArray doInBackground(String... params) {
            try {
                URL url=new URL(params[0]);

                HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.connect();

                String line;
                StringBuilder stringBuilder = new StringBuilder("");
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                JSONObject jsonObject=new JSONObject(stringBuilder.toString());
                if (jsonObject.has("next_page_token")) {
                    nextPageToken =jsonObject.getString("next_page_token");
                } else {
                    nextPageToken="";
                }
                return jsonObject.getJSONArray("results");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new JSONArray();
        }

        @Override
        protected void onPostExecute(JSONArray jsonArray) {
            progressBar.setVisibility(View.GONE);
            requestCount++;
            try {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    JSONObject location = jsonObject.getJSONObject("geometry").getJSONObject("location");

                    String placeId = jsonObject.getString("place_id");
                    String name = jsonObject.getString("name");
                    LatLng latLng = new LatLng(location.getDouble("lat"), location.getDouble("lng"));

                    GooglePlace googlePlace = new GooglePlace(name, placeId, latLng);
                    listPlaces.add(googlePlace);
                    MarkClusterItem markerClusterItem = new MarkClusterItem(latLng, name);
                    clusterManager.addItem(markerClusterItem);
                }
                clusterManager.cluster();
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (requestCount < REQUEST_LIMIT && !nextPageToken.equals("")) {
                progressBar.setVisibility(View.VISIBLE);
                String url=PLACES_REQUEST+"&pagetoken="+nextPageToken;
                new Handler().postDelayed(() -> new PlaceRequest().execute(url), 2000);
            }
        }
    }

}

错误:

2018-12-09 23:59:51.168 13210-13210/com.example.flow E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.flow, PID: 13210
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.example.flow.displayClasses.WebscrapingScreens.GoogleMapsFragment$PlaceRequest.onPostExecute(GoogleMapsFragment.java:271)
        at com.example.flow.displayClasses.WebscrapingScreens.GoogleMapsFragment$PlaceRequest.onPostExecute(GoogleMapsFragment.java:218)
        at android.os.AsyncTask.finish(AsyncTask.java:695)
        at android.os.AsyncTask.access$600(AsyncTask.java:180)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2018-12-09 23:59:51.204 1867-1881/? E/memtrack: Couldn't load memtrack module
2018-12-09 23:59:51.329 1867-1935/? E/InputDispatcher: channel '7cc9433 com.example.flow/com.example.flow.displayClasses.LoginScreens.Login (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-12-09 23:59:51.329 1867-1935/? E/InputDispatcher: channel 'a9e800c com.example.flow/com.example.flow.Home (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-12-09 23:59:52.005 1711-2262/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2018-12-09 23:59:52.059 1711-2050/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2018-12-09 23:59:54.394 1867-1983/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?).
2018-12-09 23:59:57.468 5475-5549/? E/PlayCommon: [332] com.google.android.play.b.g.a(448): Failed to connect to server: java.net.UnknownHostException: Unable to resolve host "play.googleapis.com": No address associated with hostname
2018-12-09 23:59:58.360 1867-1881/? E/memtrack: Couldn't load memtrack module
2018-12-09 23:59:58.362 1867-1881/? E/memtrack: Couldn't load memtrack module
2018-12-09 23:59:58.362 1867-1881/? E/memtrack: Couldn't load memtrack module

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.flow">
    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Eazy-Split"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        tools:ignore="GoogleAppIndexingWarning"
        tools:replace="android:icon,android:label"
        >

        <uses-library android:name="org.apache.http.legacy" android:required="false" />

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyApsuynhkRf3A7p3fgKQp01EEF8l4tggXQ" />



        <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme"
            >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Home"
            android:parentActivityName=".displayClasses.LoginScreens.Login"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".displayClasses.LoginScreens.Login" />
        </activity>

        <activity
            android:name=".displayClasses.LoginScreens.activity_signup"
            android:theme="@style/AppTheme"
            />

        <activity
            android:name=".displayClasses.LoginScreens.Login"
            android:theme="@style/AppTheme"
            />



    </application>

</manifest>

【问题讨论】:

  • 在崩溃堆栈跟踪上方查看可能来自catch 中的e.printStackTrace()doInBackground() 的跟踪。
  • 我在 catch 处设置了一个断点,但即便如此,它也会在没有获取信息的情况下崩溃。 @MikeM。
  • 好吧,nextPageToken 为空。未达到设置它的if-else。你到底把断点放在哪里了?
  • 我把它放在了'e.printStackTrace();'行。
  • 放置断点时,我注意到程序在“httpURLConnection.connect();”处停止运行。我收到的消息是“应用程序仍在运行”。 @MikeM。

标签: android google-maps android-studio nullpointerexception


【解决方案1】:

您正在返回一个几乎为空的 new JSONArray() 对象,并且尚未填充您对 doInBackground 中的实例应用的任何更改。

在您的doInBackground 中,确保创建一个本地JSONArray 对象,并将您希望在onPostExecute 方法中检索的所有实例传递给它。在您的情况下,如果您有一个名为nextPageToken 的字符串,它将在doInBackground 期间更新。然后,您将能够在onPostExecute 中检索。

执行以下操作:
doInBackground:创建本地实例:JSONArray jsonArray;
必要时写入您的对象。 例如
jsonArray = jsonObject.getJSONArray("results");

然后在你的方法doInBackground结束时,返回它:

return jsonArray;

【讨论】:

    【解决方案2】:

    @Red M 是对的。您正在返回一个空的 JSONArray。 您可以创建一个 JSONArray 写入对象并返回具有值的 JSONArray。 示例:

    JSONObject jsonObject=new JSONObject(stringBuilder.toString());
    JSONArray jsonArray = jsonObject.getJSONArray("your json value here");
    

    然后在代码的末尾:

    return jsonArray;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多