Issue
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>
Solution
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"/>
Answered By - John Pollard
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.