Note that there are some explanatory texts on larger screens.

plurals
  1. POScope Variable issue for onKeyDown event
    primarykey
    data
    text
    <p>I am having trouble using the var "str" in an if statement. I know that it is a scope problem but I am not sure how to fix it. I have failed several times.</p> <p>My goal is to use the value of "str" in an if statement to show an alert or not. Restriction is I have to assign the value of "str" this way only.</p> <pre><code> public class MyJavaScriptInterface { public String showHTML(String html) { String str = html; return str; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //Handle the back button if(keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; str =="0") { //Ask the user if they want to quit </code></pre> <h1>Update:</h1> <p>OK I was try not to bother you with reading the whole code but it's short and it might help. </p> <p>In a webview I show the html page and I also read a hidden variable which will decide if an alert box should show or not. I am just trying to get that value to the if statement in the keydown push. Here is the whole code.</p> <pre><code>package com.ishop.pizzaoven; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class buttonOne extends Activity { WebView wb = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.buttons); wb = new WebView(this); wb.getSettings().setJavaScriptEnabled(true); wb.setWebViewClient(new HelloWebViewClient()); wb.getSettings().setJavaScriptEnabled(true); wb.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT"); /* WebViewClient must be set BEFORE calling loadUrl! */ wb.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { /* This call inject JavaScript into the page which just finished loading. */ wb.loadUrl("javascript:window.HTMLOUT.showHTML(document.getElementById('sendtextcoupon').value);"); } }); wb.loadUrl("http://ishopstark.com/mobileapp.php?category=1"); setContentView(wb); } private class HelloWebViewClient extends WebViewClient { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } final Context myApp = this; public class MyJavaScriptInterface { public void showHTML(String html) { String str = html; } } public boolean onKeyDown(int keyCode, KeyEvent event) { //Handle the back button if(keyCode == KeyEvent.KEYCODE_BACK) { //Ask the user if they want to quit new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Coupon") .setMessage("Do you want a coupon texted to you?") .setPositiveButton("YES!", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Stop the activity Toast.makeText(buttonOne.this, "Great! You'll get one in just a couple of minutes.", Toast.LENGTH_LONG).show(); finish(); } }) .setNegativeButton("Not now", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Stop the activity finish(); } }) .show(); return true; } else { return super.onKeyDown(keyCode, event); } } }// End of main class </code></pre> <h1>Update 2:</h1> <p>ok so i changed my code a bit. But now I get alert errors any ideas? "The method setIcon(int) is undefined for the type buttonOne.MyJavaScriptInterface" I get this error on .setIcon(android.R.drawable.ic_dialog_alert) and return super.onKeyDown(keyCode, event);</p> <pre><code>public class MyJavaScriptInterface { public String showHTML(String html) { String str = html; return str; } public boolean onKeyDown(int keyCode, KeyEvent event, String str) { //Handle the back button if(keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; str == "0") { //Ask the user if they want to quit AlertDialog.Builder alertbox = new AlertDialog.Builder(buttonOne.this); .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Coupon") .setMessage("Do you want a coupon texted to you?") .setPositiveButton("YES!", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Stop the activity Toast.makeText(buttonOne.this, "Great! You'll get one in just a couple of minutes.", Toast.LENGTH_LONG).show(); finish(); } }) .setNegativeButton("Not now", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Stop the activity finish(); } }) .show(); return true; } else { return super.onKeyDown(keyCode, event); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload