将刷新侦听器添加到WebView的SwipeRefreshLayout时出现Null Object Reference错误

时间:2015-03-20 20:27:13

标签: android android-webview swiperefreshlayout

当我添加这行代码时

swipeLayout.setOnRefreshListener(this);

我收到此错误

void android.support.v4.widget.SwipeRefreshLayout.setOnRefreshListener
(android.support.v4.widget.SwipeRefreshLayout$OnRefreshListener)'
on a null object reference

MainActivity

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.support.v4.widget.SwipeRefreshLayout;

public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
    private WebView mWebView;
    private SwipeRefreshLayout swipeLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        mWebView = new WebView(this);

        mWebView.loadUrl("https://secure.tickspot.com");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
    }

    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onRefresh() {
        mWebView.reload();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(false);
            }
        }, 1000);
    }

}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="12" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:id="@+id/main_activity"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

我是Android开发新手,无法理解错误消息。救命! 如果我做错了什么,我也会喜欢这种反馈:)

修改

我将滑动刷新布局代码移动到

布局/ main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</android.support.v4.widget.SwipeRefreshLayout>

2 个答案:

答案 0 :(得分:0)

我不得不改变

mWebView = new WebView(this);

mWebView =(WebView)findViewById(R.id.webView);

我必须在main.xml文件中的SwipeRefreshLayout xml之间添加以下内容。

   <WebView android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"/> 

答案 1 :(得分:-2)

我已将 SwipeRefreshLayout 刷新侦听器设置为 onResume。它对我有用。