Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6135887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:32:02+00:00 2026-05-23T17:32:02+00:00

I am having trouble using the var str in an if statement. I know

  • 0

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.

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.

 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 && str =="0")
         {
             //Ask the user if they want to quit

Update:

OK I was try not to bother you with reading the whole code but it’s short and it might help.

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.

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

Update 2:

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);

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 && 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);
         }

     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:32:03+00:00Added an answer on May 23, 2026 at 5:32 pm

    Not sure exactly what your aim is, but can you not make the string a member variable?

    public class MyJavaScriptInterface  
    {  
    private String str;
     public String showHTML(String html)  
     {  
         str = html;
         return str;
        } 
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble using an argument from my class in one of that class's
I'm having trouble using Rhino Mocks to assert that a method was called (and
I'm having trouble understanding the for/in statement in JavaScript. The book which I'm using
Im having trouble using a .NET COM in vb6, It compiles ok and I
I am using mssql and am having trouble using a subquery. The real query
I am having trouble using the attribute XPath Selector in ElementTree, which I should
I'm having trouble using the flowlayoutPanel in a C# winform application. What I basically
I'm new to development using Xcode, and am having trouble using the built-in Leaks
I am using NHibernate and Rhinomocks and having trouble testing what I want. I
I'm having trouble reading a chunked response when using a StreamReader to read the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.