I know there are a lot of similar questions and believe me, I tried a lot, but nothing worked out for me. I have a Android App which uses a Webview. You can find my App in the Play Store:
https://play.google.com/store/apps/details?id=at.rk.rps&feature=search_result#?t=W251bGwsMSwxLDEsImF0LnJrLnJwcyJd
After a few month in the Play Store I decided to make an update and to use the Android Design Guidelines for my new Design, so a Actionbar was necessary. I went with ActionbarSherlock and everything is fine, except the Webview is now reloading every time I rotate the phone. In my last version I handled this problem with this answer and it worked:
My Webview Reloads When I Change From Portrait To Landscape
But now it won't work again. I have tried that solution too: (I also left a comment at the end of the site)
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
With the same disappointing result. I'm not even sure if ActionbarSherlock is the problem, because I've changed a lot, but it is impossible to recreate the old state!
This is what the App looks like (This is only in the new version implemented, you can't find it in the version which is publish on the Play Store):
Here's a part of my code, first the manifest, then the layout and afterwards the java code:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.rk.rps"
android:versionCode="8"
android:versionName="0.4" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/rps"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light.DarkActionBar" >
<activity
android:name=".RPSActivity"
android:configChanges="orientation"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/app_name"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".DonateActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".PayPalActivity"
android:configChanges="orientation"
android:label="#string/app_name" >
</activity>
<activity
android:name=".LoadingScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/paypal_loadingtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:gravity="center"
android:text="#string/paypal_loadingtext"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/rk_red" />
<ProgressBar
android:id="#+id/paypal_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/paypal_loadingtext"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp" />
<WebView
android:id="#+id/paypal_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone" />
</RelativeLayout>
Java code:
package at.rk.rps;
import org.apache.http.util.EncodingUtils;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class PayPalActivity extends SherlockActivity {
private ActionBar actionbar;
private WebView webview;
private TextView loadingtxt;
private ProgressBar progressbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getSharedPreferences(RPSActivity.PREFS_NAME, 0).getBoolean(RPSActivity.FULLSCREEN, true)) this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.paypal);
initUI();
}
private void initUI() {
actionbar = getSupportActionBar();
actionbar.setTitle(R.string.paypal_actionbar_headline);
actionbar.setHomeButtonEnabled(true);
this.actionbar.setDisplayHomeAsUpEnabled(true);
loadingtxt = (TextView) findViewById(R.id.paypal_loadingtext);
progressbar = (ProgressBar) findViewById(R.id.paypal_progressbar);
webview = (WebView) findViewById(R.id.paypal_webview);
webview.requestFocus(View.FOCUS_DOWN); // Enables the keyboard in landscape mode
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
webview.getSettings().setJavaScriptEnabled(true); // Enables JavaScript
webview.getSettings().setBuiltInZoomControls(true); // Enables zoom
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); // Displays scrollbars outside the view
webview.setScrollbarFadingEnabled(false);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
loadingtxt.setVisibility(TextView.VISIBLE);
progressbar.setVisibility(ProgressBar.VISIBLE);
webview.setVisibility(WebView.GONE);
progressbar.setProgress(progress);
if(progress == 100) {
loadingtxt.setVisibility(LinearLayout.GONE);
progressbar.setVisibility(LinearLayout.GONE);
webview.setVisibility(WebView.VISIBLE);
}
}
});
webview.setWebViewClient(new WebViewClient());
byte[] post = EncodingUtils.getBytes("cmd=_s-xclick&hosted_button_id=VRM63MEY4J936", "BASE64");
webview.postUrl("https://www.paypal.com/cgi-bin/webscr", post);
}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true){
webview.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater i = getSupportMenuInflater();
i.inflate(R.menu.paypal_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
finish();
return(true);
case R.id.paypal_actionbar_reload:
webview.reload();
return(true);
default:
return super.onOptionsItemSelected(item);
}
}
}
I found the answer here:
https://stackoverflow.com/a/9550231/1254514
"Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation."
So I had to do the following:
Add in your Android manifest to the activity you like:
android:configChanges="keyboardHidden|orientation|screenSize"
Then override the function in your activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Maybe if you try something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getSharedPreferences(RPSActivity.PREFS_NAME, 0).getBoolean(RPSActivity.FULLSCREEN, true)) this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.paypal);
initUI();
if(savedInstanceState != null)
{
/** Restoring the web Ui state. */
webView.restoreState(savedInstanceState);
}
}
#Override
public void onSaveInstanceState(Bundle outState)
{
/** Saving the webview state. */
webView.saveState(outState);
}
This is untested but it seems like you are not saving the state of the webview. I am using this code in a Fragment and it seems to save the state of the webview.
Related
I'm a rookie at android, trying to make webapp, by following a tutorial.
However, when installed a signed apk, after opening it gives a blank page. have no idea what went wrong.
My edited pages are:
Activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
tools:context=".MainActivity">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iinfohub.www.iinfohub">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="IINFO HUB"
android:roundIcon="#drawable/logo"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<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>
MainActivity.java
package com.iinfohub.www.iinfohub;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView mWebView;
SwipeRefreshLayout swipe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
public void onRefresh() {
LoadWeb();
}
});
LoadWeb();
}
public void LoadWeb() {
mWebView = (WebView) findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.loadUrl("http://iinfohub.com/forapp/index.php");
swipe.setRefreshing(true);
mWebView.setWebViewClient(new WebViewClient() {
public void onReveivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file://android_asset/error.html");
}
public void onPageFinished(WebView view, String url) {
//hide the swipe refreshlayout
swipe.setRefreshing(false);
}
});
}
#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);
}
}
All i did was follow that tutorial, as i mentioned, it gives a blank page on running the signed APK.
Any help on this is greatly appreciated.
I think you should loadUrl after all.
because settings/configs must apply before loading page.
I develop an app with WebView and Html5, It works, clicking a link in the webpage goes to the next page in the html file inside my app. But when I click the phone's back button, it give me this error : Unfortunately, APPNAME has stopped. I want to go back to the previous page in the website instead.
The website build with : https://github.com/01org/appframework
MainActivity.java
package com.package.name;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView WebView;
//Back Button Code
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (WebView.canGoBack()) {
WebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
//--------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView=(WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(true);
webView.loadUrl("file:///android_asset/index.html");
}
}
Activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.package.name.MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</WebView>
</android.support.constraint.ConstraintLayout>
AndroidManifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.name">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<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>
First,
Check if WebView is null.. rename the global variable to webView (private WebView webView;) and in onCreate change WebView webView=(WebView) findViewById(R.id.webview); to webView=(WebView) findViewById(R.id.webview);
You could also try moving the back button handling code to
#Override
public void onBackPressed()
{
....
}
instead of
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
...
}
as in:
#Override
public void onBackPressed()
{
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
as mentioned in https://developer.android.com/training/implementing-navigation/temporal.html#back-webviews
i have problems in playing my video. I have an activity with the following property in manifest
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
but everytime i change the orientation from portrait to landscape, the screen size is showing portrait. and the same thing happening for Landscape to portrait the screen size is showing landscape like the figure shown in the link.
enter image description here
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bdb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.bdb;
import android.app.Activity;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams .FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView myVideoView = (VideoView)findViewById(R.id.birthdayview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(myVideoView);
try{
String vidpath = "android.resource://" + getPackageName() + "/" + R.raw.birthday;
myVideoView.setVideoURI(Uri.parse(vidpath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
catch (Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT);
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context="com.example.bdb.MainActivity" >
<VideoView
android:id="#+id/birthdayview"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
</VideoView>
any solution/suggestions to solve this problem? Thank you so much :)
By declaring android:configChanges="orientation|keyboard|keyboardHidden|screenSize" in your manifest, you took responsibility for handling all these changes. Yet, you have not implemented onConfigurationChanged to handle the configuration change for your layout. You should manually change the size of the VideoView.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
doLayout();
}
private void doLayout() {
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_PORTRAIT) {
// do PORTRAIT stuff
} else {
// do LANDSCAPE stuff
}
}
I am trying to create a screen filter applications.
so I'm looking for a way to change the color of the screen.
Looking for a lot of web sites, it is hard to find a way that I think.
First, how to gain access to the system, but is not suitable for distribution.
I think the second way to replace or overlay a color, it is not easy.
There comes a lot of pages to introduce the application.
The layout was created.
Now find a way.
I just do not see the page?
like this link :
https://play.google.com/store/apps/details?id=nu.bluelight.filter
Android Permission to Change Screen Brightness
.
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:max="10" />
</RelativeLayout>
MainActivity.java
package com.example.android_adjustbrightness;
import android.os.Bundle;
import android.provider.Settings.SettingNotFoundException;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private SeekBar seekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setMax(255);
float curBrightnessValue = 0;
try {
curBrightnessValue = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
int screen_brightness = (int) curBrightnessValue;
seekBar.setProgress(screen_brightness);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
progress = progresValue;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here,
// if you want to do anything at the start of
// touching the seekbar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_adjustbrightness"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" >
</uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.android_adjustbrightness.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
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>
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"/>
I have set SwipeRefreshLayout refresh listener into onResume. It worked for me.