android - Null Object Reference error when adding refresh listener to SwipeRefreshLayout for a WebView -
when add line of code
swipelayout.setonrefreshlistener(this);
i error
void android.support.v4.widget.swiperefreshlayout.setonrefreshlistener (android.support.v4.widget.swiperefreshlayout$onrefreshlistener)' on 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 android development , having trouble understanding error messages. help! if i'm doing else wrong, love feedback :)
edit
i moved swipe refresh layout code
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>
i had change
mwebview = new webview(this);
to
mwebview = (webview) findviewbyid(r.id.webview);
and had add following between swiperefreshlayout xml in main.xml file.
<webview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webview" android:layout_alignparenttop="true"/>
Comments
Post a Comment