无法在onLoadFinished()中调用TextView.SetText

时间:2018-12-08 09:02:48

标签: java android loader

作为udacity课程的一部分,我目前正在研究示例应用程序。我正在尝试从TextView.setText方法中调用onLoadFinished方法,在实现加载程序的过程中我已经对其进行了覆盖。 TextView的目的是当ListView没有内容时成为空状态占位符。参见下面的Java类:

   /*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.quakereport;

import android.app.LoaderManager;
import android.content.Intent;
import android.content.Loader;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class EarthquakeActivity extends AppCompatActivity
        implements LoaderManager.LoaderCallbacks<List<Earthquake>> {

    public static final String LOG_TAG = EarthquakeActivity.class.getName();
    private static final String USGS_QUERY_URL =
            "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake&orderby=time&minmag=6&limit=10";
    private static final int EARTHQUAKE_LOADER_ID = 1;
    private EarthquakeAdaptor mAdaptor;
    private TextView mEmptyStateTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);

        // Find a reference to the {@link ListView} in the layout
        ListView earthquakeListView = (ListView) findViewById(R.id.list);
        mEmptyStateTextView = (TextView) findViewById(R.id.empty_text_view);
        earthquakeListView.setEmptyView(mEmptyStateTextView);

        mAdaptor = new EarthquakeAdaptor(this, new ArrayList<Earthquake>());

        earthquakeListView.setAdapter(mAdaptor);
        LoaderManager loaderManager = getLoaderManager();
        loaderManager.initLoader(EARTHQUAKE_LOADER_ID,null,this);
        Log.i("LoaderMonitoring","initLoader() called");

        earthquakeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Earthquake earthquake = mAdaptor.getItem(position);
                String url = earthquake.getUrl();
                Log.i("EarthquakeActivity","value of url is: " + url);
                Intent openURLInBrowser = new Intent(Intent.ACTION_VIEW);
                openURLInBrowser.setData(Uri.parse(url));
                startActivity(openURLInBrowser);
            }
        });
    }

    @Override
    public Loader<List<Earthquake>> onCreateLoader(int id, Bundle bundle) {
        // TODO: Create a new loader for the given URL
        Log.i("LoaderMonitoring","onCreateLoader called");
        return new EarthquakeLoader(this,USGS_QUERY_URL);
    }

    @Override
    public void onLoadFinished(Loader<List<Earthquake>> loader, List<Earthquake> earthquakes) {
        // TODO: Update the UI with the result
        Log.i("LoaderMonitoring","onLoadFinished called");
        mAdaptor.clear();

        if(earthquakes != null && !earthquakes.isEmpty()) {
            mAdaptor.addAll(earthquakes);
        }
        mEmptyStateTextView.setText(R.string.empty);
    }

    @Override
    public void onLoaderReset(Loader<List<Earthquake>> loader) {
        Log.i("LoaderMonitoring","onLoaderReset called");
        mAdaptor.clear();
    }
}

mEmptyStateTextView.setText(R.string.empty);是令人讨厌的代码行。下面是堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.quakereport, PID: 11673
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
        at com.example.android.quakereport.EarthquakeActivity.onLoadFinished(EarthquakeActivity.java:90)
        at com.example.android.quakereport.EarthquakeActivity.onLoadFinished(EarthquakeActivity.java:33)
        at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:489)
        at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:457)
        at android.content.Loader.deliverResult(Loader.java:144)
        at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:268)
        at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:92)
        at android.os.AsyncTask.finish(AsyncTask.java:695)
        at android.os.AsyncTask.-wrap1(Unknown Source:0)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

任何帮助将不胜感激, 谢谢。

0 个答案:

没有答案