Android NullPointerException on WebView goBack() method - java

I am using Google Analytics for my android applications and see NullPointerException on the line "wvBrowser.goBack();" although I check it before if the wvBrowser(WebView) is null. I can't make it happen on my normal tests. Any idea?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if (wvBrowser!=null) {
if(wvBrowser.canGoBack() == true){
wvBrowser.goBack();
return true;
}else{
}
}
}
}
return super.onKeyDown(keyCode, event);
}

Related

Im not Getting the Button Release Toast in OnTouchListners

The issue is im not getting the Button release Toast.
i've a simple view in xml on which im performing onTouch.
hidenBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
firstTime=System.currentTimeMillis();
Toast.makeText(MainActivity.this, "Pressed", Toast.LENGTH_SHORT).show();
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
Toast.makeText(MainActivity.this, "Released", Toast.LENGTH_SHORT).show();
secondTime=System.currentTimeMillis();
if(secondTime-firstTime>=5000){
//do your actions here,prev,curr are fields in a class
ShowDialog();
}
else{
firstTime=0;
secondTime=0;
}
}
// TODO Auto-generated method stub
return false;
}
});
Change your OnTouchListener like this:
hidenBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
firstTime=System.currentTimeMillis();
Toast.makeText(MainActivity.this, "Pressed", Toast.LENGTH_SHORT).show();
return true;
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
Toast.makeText(MainActivity.this, "Released", Toast.LENGTH_SHORT).show();
secondTime=System.currentTimeMillis();
if(secondTime-firstTime>=5000){
//do your actions here,prev,curr are fields in a class
ShowDialog();
}
else{
firstTime=0;
secondTime=0;
}
}
// TODO Auto-generated method stub
return false;
}
});
You are returning false at ACTION_DOWN which means you are not consuming your touch event, so no other steps of the same event are triggered. Therefore you need to return true in the ACTION_DOWN so you can intercept ACTION_UP.

How to implement onKeyDown in Fragment

I change all my activitys to Fragment. I got stuck in the onkeydown part.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (!sector.getText().toString().equals("Sector") && (media.isChecked() || completa.isChecked())) {
siguiente.setEnabled(true);
} else {
siguiente.setEnabled(false);
}
return super.onKeyDown(keyCode, event);
}
You can do this by defining method in your fragment class. For example:
public void onMyKeyDown(int key, KeyEvent event){
//define your statement like
if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5
&& key == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to Exit?")
.setIcon(R.drawable.logo)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}})
.setNegativeButton(android.R.string.no, null).show();
}
}
Call onMyKeyDown method whenever a key-down event is raised in your Activity class. example:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//call fragment method onMyKeyDown(keyCode, event)
return super.onKeyDown(keyCode, event);
}

How to prevent GOBACK button for specific URL in Android Webview?

I have an web application that work on my Android WebView with out problem.
Here is my Android app launcher code:
public class MainActivity extends Activity {
WebView webView;
private String urlAddress = "http://www.YourDomainName.com/TheLoginPage";
private String loadingTxt = "Loading";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast loadingMsg = Toast.makeText(this,
loadingTxt, Toast.LENGTH_SHORT);
loadingMsg.show();
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(urlAddress);
webView.setWebViewClient(new myWebViewClient());
webView.setInitialScale(1);
webView.getSettings().setUseWideViewPort(true);
}
#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();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Every thing is working.
My Question: the default start page in the WebView is the web application login page. After a successful login process I want to prevent (build in) GO BACK button to back page to the default start page (login page) since I am successfully logged in?
Note: I will still use the GO BACK button to the rest of the web application or if I logout out again.
Here how I solved it, I have added if equals statement to compare the URL of the page that comes after the login process with the WebView URL, if they are identical then GO BACK is disallowed and a message "You are logged in", that way I prevent GO BACK. Here is the code and it works for me:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
String url = new String("http://www.YourDomainName.com/PageAfterSuccessfulLogin");
String webUrl = new String(webView.getUrl());
if (url.equals(webUrl)) {
Toast.makeText(this, "You are logged in",
Toast.LENGTH_SHORT).show();
} else {
if (webView.canGoBack()) {
Toast.makeText(this, "Back",
Toast.LENGTH_SHORT).show();
webView.goBack();
}
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thanks maytham-maytham, it works charmly.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
String url = new String("https://www.domain name.com/index.php?logged=login%20successfully");
String webUrl = new String(webView.getUrl());
if (url.equals(webUrl)) {
Toast.makeText(this, "You are logged in",
Toast.LENGTH_SHORT).show();
}
else {
url = new String("https://www.domain name.com/main.php");
webUrl = new String(webView.getUrl());
if (url.equals(webUrl)) {
Toast.makeText(this, "Access Denied",
Toast.LENGTH_SHORT).show();
}
else
if (webView.canGoBack()) {
Toast.makeText(this, "Back",
Toast.LENGTH_SHORT);
webView.goBack();
}
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
In which ever activity you want the back button to be disabled - DO THIS
#Override
public void onBackPressed(){
return;
}
You have to add this as a global function.
As in your case you want your back button to work in some cases and not work in some-
You can do this inside the onBackPressed()
if(BackButton_Dont_Work_Condition)
return
else
super.onBackPressed()

dispatchKeyEvent calling method twice

I've implemented dispatchKeyEvent in my activity to listen to the Enter key being pressed.
The problem is that when i click enter,it calls my method twice ? How can i fix this ?
Thanks,have a nice day !
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
enter();
return true;
}
return super.dispatchKeyEvent(e);
};
Fixed it,done this :
At first i was doing ACTION_DOWN but that was triggering an older problem of mine.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};

Android How to listen for Volume Button events?

I know you guys are probably tired of these kinds of posts, but why doesn't anything happen when I press volume down? I'm just trying to make a simple code, but apparently it's not working.
package com.cakemansapps.lightwriter;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.view.KeyEvent;
import android.util.Log;
public class LightWriter extends Activity implements OnTouchListener {
private static final String TAG = "Touch" ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
Log.w("LightWriter", "I WORK BRO.");
return true;
}
return super.onKeyLongPress(keyCode, event);
}
public boolean onTouch(View view, MotionEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
I don't know if you can get long press events for the hardware keys.
I've used this code to listen for the volume button before.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
// Do something
}
return true;
}
If that doesn't work for you let us know what device you are testing on.
Kotlin
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
// Do something
}
return true
}
Another approach
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
// ... your code
return true;
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
// ... your code
return true;
} else
return super.onKeyDown(keyCode, event);
}
try these. just tested them:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
super.onKeyLongPress(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
Log.w("LightWriter", "I WORK BRO.");
return true;
}
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
Log.w("LightWriter", "I WORK BRO.");
return true;
}
return true;
}
use this code to handle Volume key event
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
super.onKeyUp(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
Toast.makeText(MainActivity.this,"Up working",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
Toast.makeText(MainActivity.this,"Down working",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}

Categories