0

When I add this line of code

swipeLayout.setOnRefreshListener(this);

I get this error

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>

Im new to android development and am having trouble understanding the error messages. HELP! If I'm doing anything else wrong, I would love that feedback too :)

EDIT

I moved the swipe refresh layout code to

layout/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>
John Pollard
  • 3,729
  • 3
  • 24
  • 50
  • why did you put a Layout in the manifest? – Blackbelt Mar 20 '15 at 20:33
  • You don't put your layout in your manifest... – Dave S Mar 20 '15 at 20:33
  • A layout. In a manifest. Never seen this one before :') – OcuS Mar 20 '15 at 20:36
  • Should I put the code from the manifest into the layout/main.xml file? – John Pollard Mar 20 '15 at 20:40
  • 1
    You should put it in the layout associated with your view (which is *none* by now). – OcuS Mar 20 '15 at 20:42
  • okay so i should add "setContentView(R.layout.main);" to the MainActivity correct? – John Pollard Mar 20 '15 at 20:49
  • 1
    yes setContentView, yes put it in layout/main.xml – Dave S Mar 20 '15 at 20:52
  • I added the setContent and moved my swipe refresh layout code the the main.xml layout file. I made an edit to the post to show what I did. Still doesnt appear to be working. When I run the code i get this error a lot 03-19 23:52:32.740 8162-8201/com.tick.android E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000b44 03-19 23:52:32.741 8162-8201/com.tick.android E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0 03-19 23:52:32.743 8162-8201/com.tick.android E/eglCodecCommon﹕ **** ERROR unknown type 0x0 (glSizeof,72) – John Pollard Mar 20 '15 at 21:04
  • http://stackoverflow.com/questions/22348801/phonegap-eclipse-issue-eglcodeccommon-glutilsparamsize-unknow-param-errors – Dave S Mar 20 '15 at 21:06
  • I think the link is suggesting that you're using an emulator and you should uncheck "use host gpu". At least that's what it says on the last answer with the many upvotes. Anyway, is the app even running at all? – MingShun Mar 20 '15 at 21:09
  • 1
    this.setContentView(mWebView); isn't what you want to be doing, you should put whatever you want displayed in your main layout, including the webview – Dave S Mar 20 '15 at 21:11
  • 1
    I definitely suggest following a tutorial, you are making mistakes that only happen without a basic understanding of how Android Development works. – Dave S Mar 20 '15 at 21:13
  • 1
    The basics are important, but your usage of swiperefreshlayout also requires more specialized research. I found this with a google search. Try removing the mWebView and trying this first: http://antonioleiva.com/swiperefreshlayout/ – MingShun Mar 20 '15 at 21:16

2 Answers2

-1

I had to change

mWebView = new WebView(this);

to

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

And I had to add the following between the SwipeRefreshLayout xml in the main.xml file.

   <WebView android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"/> 
John Pollard
  • 3,729
  • 3
  • 24
  • 50
-2

I have set SwipeRefreshLayout refresh listener into onResume. It worked for me.