How to prevent webview from reloading on rotate - android

I'm trying to implement a webview that on rotate maintains the current page and page data. I have dynamic content being loaded to the client, and they have to start all over if the screen rotates and reloads the page.
I've followed other stackoverflow questions (mostly this one: Android - Preventing WebView reload on Rotate), but I'm not having any luck.
I override onSaveInstanceState and onRestoreInstanceState saving and restoring web_view, but it's not working for me. I also check if savedInstanceState is null or not and when it is I load a url.
Here are the files to my project:
Main.java
package com.mypackage.name;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import static android.content.Intent.ACTION_VIEW;
/**
* Created by Admin on 2/15/2018.
*/
public class main extends AppCompatActivity {
WebView web_view;
SwipeRefreshLayout swipe_refresh_layout;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web_view = (WebView) findViewById(R.id.web_view);
swipe_refresh_layout = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
CookieManager cookie_manager = CookieManager.getInstance();
cookie_manager.setAcceptCookie(true);
WebSettings web_settings = web_view.getSettings();
web_settings.setJavaScriptEnabled(true);
web_settings.setAppCacheEnabled(true);
web_settings.setJavaScriptCanOpenWindowsAutomatically(true);
web_settings.setSavePassword(false);
if (savedInstanceState == null) {
web_view.setWebViewClient(new UriWebViewClient());
web_view.loadUrl("https://example.com/my_web_site/");
}
else {
web_view.restoreState(savedInstanceState);
}
swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
web_view.reload();
swipe_refresh_layout.setRefreshing(false);
}
});
// https://stackoverflow.com/questions/37775213/adding-pull-to-refresh-on-webview-for-refreshing
// https://stackoverflow.com/questions/12131025/android-preventing-webview-reload-on-rotate
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
web_view.saveState(outState);
Log.d("SAVE", "here");
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web_view.restoreState(savedInstanceState);
Log.d("RESTORE", "here");
}
private class UriWebViewClient extends WebViewClient {
#Override
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.name">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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=".main" >
<intent-filter>
<action android:name="android.intent.action.MAIN"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout 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=".main"
android:id="#+id/webview_frame" >
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

Try this in your webview settings
web_settings.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
also you need to Save the state of the web view when the screen is rotated.
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
m_webview.saveState(outState);
}
and then reload the old WebView content before you call for web_settings
if (savedInstanceState != null) {
m_webview.restoreState(savedInstanceState);
}

Related

web app gives out blank screen on installing and opening?

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.

pdf reader from assets folder

I want to create a pdf reader with android studio. I added a pdf file to assets folder. I used seekbar. But code doesn't show anything. What's wrong?
I share all prog
Mainactivity.java
package enespolat.polat.com.kuranahmethusrev;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.pdf.PdfRenderer;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import uk.co.senab.photoview.PhotoViewAttacher;
public class MainActivity extends AppCompatActivity {
LinearLayout pagerSection;
SeekBar pageSeekbar;
ImageView pageView;
TextView tvCounter;
PdfRenderer.Page sayfa;
PhotoViewAttacher pva;
int pageCount=0;
int currPage=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pageSeekbar=(SeekBar)findViewById(R.id.sbPager);
pagerSection=(LinearLayout)findViewById(R.id.llPageCounter);
pageView=(ImageView)findViewById(R.id.iv);
tvCounter=(TextView)findViewById(R.id.tvCounter);
render();
pva = new PhotoViewAttacher(pageView);
pva.setMaximumScale(10.0f);
pageSeekbar.setMax(605);
pageSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b)
{
currPage=i;
showCurrentPageIndicator();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
currPage=seekBar.getProgress();
render();
}
});
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (pageView != null)
pageView.invalidate();
}
void showCurrentPageIndicator()
{
tvCounter.setText(""+(currPage+1));
pagerSection.setVisibility(View.VISIBLE);
pagerSection.postDelayed(new Runnable() {
#Override
public void run() {
pagerSection.setVisibility(View.GONE);
}
},700);
}
private void render()
{
try {
File dlFolder = new File(Environment.getExternalStorageDirectory(), "Download");
File pdfFile = new File(dlFolder, "kuran.pdf");
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(pdfFile,ParcelFileDescriptor.MODE_READ_ONLY));
sayfa = renderer.openPage(currPage);
Bitmap bitmap=Bitmap.createBitmap(sayfa.getWidth(),sayfa.getHeight(), Bitmap.Config.ARGB_8888);
Matrix m=pageView.getImageMatrix();
m.setScale(1.0f, 1.0f);
Rect rect=new Rect(0,0,sayfa.getWidth(),sayfa.getHeight());
sayfa.render(bitmap,rect,m,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
//img.setImageMatrix(m);
pageView.setImageBitmap(bitmap);
pageView.invalidate();
/**/
sayfa.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
layout folder
<?xml version="1.0" encoding="utf-8" ?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="#+id/activity_main" android:layout_width="match_parent" android:background="#FFFFFF" android:layout_height="match_parent">
<ImageView android:id="#+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="#+id/ly" />
- <LinearLayout android:background="#88333333" android:layout_width="match_parent" android:paddingRight="5dp" android:paddingLeft="5dp" android:id="#+id/ly" android:paddingBottom="12dp" android:paddingTop="12dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true">
<SeekBar android:id="#+id/sbPager" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
- <LinearLayout android:visibility="gone" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="40dp" android:background="#88333333" android:id="#+id/llPageCounter" android:layout_centerVertical="true" android:layout_centerHorizontal="true">
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="#FFFFFF" android:textStyle="bold" android:id="#+id/tvCounter" android:text="355" android:textSize="40sp" android:gravity="center" />
</LinearLayout>
</RelativeLayout>
manifest
<?xml version="1.0" encoding="utf-8" ?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="enespolat.polat.com.kuranahmethusrev">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="#string/app_name" 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>

Numeric login app does not switch activities?

Hi I have a basic app for logging in with hardcoded credentials, and I want it to take you to another activity called Welcome. Here is my main activity:
package com.example.numericlogin;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity{
private EditText login_key=null;
private Button login=null;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login_key = (EditText)findViewById(R.id.editText1);
login = (Button)findViewById(R.id.login);
}
public void login(View view){
if(login_key.getText().toString().equals("123456")){
Toast.makeText(getApplicationContext(), "Login Successful!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this,Welcome.class));
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
{
login.setEnabled(false);
}
}
}
#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;
}
}
Here is the main activity's 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.numericlogin.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/login_key" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView1"
android:ems="10"
android:digits="0123456789"
android:inputType="number|textPassword"
android:maxLength="6"
android:password="true"
/>
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="#string/login" />
</RelativeLayout>
Here is the second activity I want it to switch to when logged in correctly:
package com.example.numericlogin;
import android.view.View;
import android.widget.ImageView;
public class Welcome {
public void welcome(View v){
ImageView picture = (ImageView)
setContenView(R.layout.fragment_main);
}
private ImageView setContenView(int fragmentMain) {
// TODO Auto-generated method stub
return null;
}
}
Here is the xml for the above activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:src="#drawable/welcome" />
</RelativeLayout>
And here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.numericlogin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.numericlogin.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>
<activity android:name=".Welcome"/>
</application>
</manifest>
At the moment when I launch it it brings up the first activity and allows you to enter input in the login area, but when you click the login button nothing happens. There are no errors in the code and here is the logcat output:
07-01 14:11:15.313: W/IInputConnectionWrapper(23128): showStatusIcon on inactive InputConnection
This is how you switch between activities:
In MainActivity.java:
Intent go = new Intent(this, Welcome.class);
startActivity(go);
Your second activity class must extend Activity
public class Welcome extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.childact);
}
}
And you must register all activities in manifest file.
You are missing this:
<activity
android:name="com.example.testingproj.Welcome"
android:label="#string/app_name">
</activity>
I think you forgot set onClickListener for the login button or set attribute onClick with value 'login' in the properties panel in graphical layout preview.
final Button login= (Button) findViewById(R.id.login);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform code validation
}
});

Unfortunately projects has stopped: onClickListener

When I open the app it says "Unfortunately program has stopped". So i can't use the application. How can i repair this?
This is my code:
I put the incorrect code, this is the real code, same error
package com.RaulLara.projects;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.app.Activity;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button btn = (Button) findViewById(R.id.btn1);
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
setupmybutton();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
private void setupmybutton() {
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
#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;
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
This is my
fragment_main:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.RaulLara.projects.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:text="#string/button" />
</RelativeLayout>
And this is my
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.RaulLara.projects"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.RaulLara.projects.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>
</manifest>
You are using wrong id for the button. This is causing a NULL mybutton1 to be returned and hence a null pointer exception.
Change
Button mybutton1 = (Button) findViewById(R.id.button1);
to
Button mybutton1 = (Button) findViewById(R.id.btn1);
Good Luck
Since you are setting up your button in OnCreate of the MainActivity make sure your declare your btn1 in activity_main and not in the fragment's layout fragment_main

WebView isn't opening my me web but in browser works

I read a lot of posts like this here, but didn't work for me...
It always shows me "Webpage not available"
activity.java
package ar.com.liit.datawarehouse;
import ar.com.liit.datawarehouse.util.SystemUiHider;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class FullscreenActivity extends Activity {
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
private SystemUiHider mSystemUiHider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
final View contentView = findViewById(R.id.fullscreen_content);
mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
mSystemUiHider.setup();
}
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
delayedHide(100);
final WebView wBrowser = (WebView) findViewById(R.id.webView1);
wBrowser.getSettings().setJavaScriptEnabled(true);
wBrowser.loadUrl("https://liit.com.ar/");
wBrowser.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
wBrowser.loadUrl("https://www.google.com/");
}
});
}
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
mSystemUiHider.hide();
}
};
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ar.com.liit.datawarehouse"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:hardwareAccelerated="true">
<activity
android:name="ar.com.liit.datawarehouse.FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
layout
<FrameLayout 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:background="#0099cc"
tools:context=".FullscreenActivity" >
<!--
The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc.
-->
<TextView
android:id="#+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="#string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows.
-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</FrameLayout>
it seems to be a well know "issue". The "fix" would be to override WebViewClient.onReceivedSslError, and use handler.proceed()

Categories

Resources