• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Button direct to webpage wont work.

sigurros

Newbie
Jul 22, 2010
27
0
I have created an app who's menu has four buttons. Each link to a different webpage, however only the first button actually works. Any help or info is greatly appreciated. I have posted the java code and manifest below. If i need to post more please let me know.

Menu JAVA

Code:
package android.bankclosures;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;


public class MenuActivity extends Activity {
    WebView mWebView;
    WebView mWebView1;
    WebView mWebView2;
    WebView mWebView3;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        
       Button Button01=(Button)findViewById(R.id.Button01);
       Button01.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent IntentlaunchActivity = new Intent(MenuActivity.this, Clsdbanks.class);
            startActivity(IntentlaunchActivity);
            
            
        Button Button02=(Button)findViewById(R.id.Button02);
        Button02.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent IntentlaunchActivity1 = new Intent(MenuActivity.this, Press.class);
                startActivity(IntentlaunchActivity1);
                
        Button Button03=(Button)findViewById(R.id.Button03);
        Button03.setOnClickListener(new View.OnClickListener() {
                    
            @Override
            public void onClick(View v) {
                Intent IntentlaunchActivity2 = new Intent(MenuActivity.this, Form.class);
                startActivity(IntentlaunchActivity2);
                        
                
        Button Button04=(Button)findViewById(R.id.Button04);
        Button04.setOnClickListener(new View.OnClickListener() {
                    
            @Override
            public void onClick(View v) {
                Intent IntentlaunchActivity3 = new Intent(MenuActivity.this, News.class);
                startActivity(IntentlaunchActivity3);
                    finish();
                    }
                    });
                
            }
        });
        }

        
              
        

            
        
    });
        

    
        

    }
 
        
    });
    }
}



Working Activity Java

Code:
package android.bankclosures;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Clsdbanks extends Activity {
    WebView mWebView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clsdbanks);
        
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.fdic.gov/bank/individual/failed/banklist.html");
        
 class ClsdbanksClient extends WebViewClient {
              @Override 
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  view.loadUrl(url);
                  return true;
              }
          }
          mWebView.setWebViewClient(new ClsdbanksClient());
    }
          @Override 
          public boolean onKeyDown(int keyCode, KeyEvent event) {
              if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                  mWebView.goBack();
                  return true;
          }
          return super.onKeyDown(keyCode, event);
    }
          
    }



Non Working JAva

Code:
package android.bankclosures;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Press extends Activity {
    WebView mWebView1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.press);
        
        mWebView1 = (WebView) findViewById(R.id.webview);
        mWebView1.getSettings().setJavaScriptEnabled(true);
        mWebView1.loadUrl("http://www.fdic.gov/bank/historical/bank/index.html");
        
 class PressClient extends WebViewClient {
              @Override 
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  view.loadUrl(url);
                  return true;
              }
          }
          mWebView1.setWebViewClient(new PressClient());
    }
          @Override 
          public boolean onKeyDown(int keyCode, KeyEvent event) {
              if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView1.canGoBack()) {
                  mWebView1.goBack();
                  return true;
          }
          return super.onKeyDown(keyCode, event);
    }
          
    }



MAnifest

Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="android.bankclosures"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="@string/app_name" android:debuggable="true" android:enabled="true" android:icon="@drawable/logonobg">
        

    <activity android:name=".SplashActivity" 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=".MenuActivity" android:label="Menu">
</activity>


<activity android:label="Closed Banks" android:name=".Clsdbanks"></activity>
<activity android:label="Press" android:name=".Press"></activity>
<activity android:name=".Form" android:label="Help Form"></activity>
<activity android:name=".News" android:label="RK News"></activity>
</application>
    <uses-sdk android:minSdkVersion="8" />


</manifest>
 
Change your MenuActivity to this:

Code:
public class MenuActivity extends Activity implements OnClickListener{
    WebView mWebView;
    WebView mWebView1;
    WebView mWebView2;
    WebView mWebView3;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        
       Button Button01=(Button)findViewById(R.id.Button01);
       Button Button02=(Button)findViewById(R.id.Button02);
       Button Button03=(Button)findViewById(R.id.Button03);
       Button Button04=(Button)findViewById(R.id.Button04);
       Button01.setOnClickListener(this);
       Button02.setOnClickListener(this);
       Button03.setOnClickListener(this);
       Button04.setOnClickListener(this);
    }
    		   
    	
        public void onClick(View v) {
            Intent intentLaunchActivity;
            switch(v)
            {
            case Button01:
            	IntentlaunchActivity = new Intent(MenuActivity.this, Clsdbanks.class);
            	startActivity(IntentlaunchActivity);
            	break;
            case Button02:
            	IntentlaunchActivity = new Intent(MenuActivity.this, Press.class);
                startActivity(IntentlaunchActivity);
                break;
            case Button03:
            	IntentlaunchActivity = new Intent(MenuActivity.this, Form.class);
                startActivity(IntentlaunchActivity);
                break;
            case Button04:
            	IntentlaunchActivity = new Intent(MenuActivity.this, News.class);
                startActivity(IntentlaunchActivity);
                finish();
                break;
            }
        }   
}

EDIT: It seems like it should have worked the way you had it, but try it this way just to be safe.
 
  • Like
Reactions: sigurros
Upvote 0
Ah wait, in your manifest change the activity definitions to:

Code:
<activity android:label="Closed Banks" android:name=".Clsdbanks"></activity>
<activity android:label=".Press" android:name=".Press"></activity>
<activity android:name=".Form" android:label="Help Form"></activity>
<activity android:name=".News" android:label="RK News"></activity>

You forgot the "." in the Press activity's name property.
 
  • Like
Reactions: sigurros
Upvote 0
Thank you. The missing . was why the page wouldn't load but I ran into another problem as well. The links only work if I click the top one first, then the second etc. So If i want to press the last one, I have to visit the top 3 in order then the last one.

This I have no clue how to fix. Also I tried to use your code but got a lot of errors.
 
Upvote 0
In MENU Java, I think you're defining the onClick methods cascading. That is, you only attach the event listener for the second button inside the onClick method for the first button, so the listener doesn't get set untill you click the first button. That's why you can only access the pages in that order. What jonbonazza suggested should work better since, there setOnClickListener gets called in onCreate() so as soon as the app starts.
 
  • Like
Reactions: sigurros
Upvote 0
Thank you. The missing . was why the page wouldn't load but I ran into another problem as well. The links only work if I click the top one first, then the second etc. So If i want to press the last one, I have to visit the top 3 in order then the last one.

This I have no clue how to fix. Also I tried to use your code but got a lot of errors.

What errors are you getting using my code? Using my code should fix the cascading errors if you can get it to work.
 
  • Like
Reactions: sigurros
Upvote 0
Any help or info is greatly appreciated. I have posted the java code and manifest below.

Just a quick suggestion, in case it's of any use.

If you go into your MenuActivity class in Eclipse and type SHIFT+CTRL+F it will reformat the code and you'll get a better idea about its actual structure. The current indentation is misleading and makes it hard to spot that one button listener is defined inside another button listener, which is defined inside another.... (As pointed out already by maest)

You might not like the result of the auto-formatter, so make sure you save the code before you reformat, so you can go back if you don't like it.

Mark
 
  • Like
Reactions: sigurros
Upvote 0
I also need to package this up so it can be tested on an android phone that i have to send to via email, any clue where to go to learn to do that.

Export an application package file (.apk).

Right click on your project, then Android Tools -> Export Unsigned Package
Or you can export a Signed package instead, which is what you'd do if you were releasing the app to the Adroid Market. (I've never tried to run an unsigned package on a phone, but I assume it would work. It's not much more work to export a signed package anyway.)

Mark
 
  • Like
Reactions: sigurros
Upvote 0
Oh, woops.
This should fix it:

Code:
public class MenuActivity extends Activity implements OnClickListener{
    WebView mWebView;
    WebView mWebView1;
    WebView mWebView2;
    WebView mWebView3;
    Button Button01;
    Button Button02;
    Button Button03;
    Button Button04;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        
       Button01=(Button)findViewById(R.id.Button01);
       Button02=(Button)findViewById(R.id.Button02);
       Button03=(Button)findViewById(R.id.Button03);
       Button04=(Button)findViewById(R.id.Button04);
       Button01.setOnClickListener(this);
       Button02.setOnClickListener(this);
       Button03.setOnClickListener(this);
       Button04.setOnClickListener(this);
    }
    		   
    	
        public void onClick(View v) {
            Intent intentLaunchActivity;
            
            if(v == Button01)
            {
            	IntentlaunchActivity = new Intent(MenuActivity.this, Clsdbanks.class);
            	startActivity(IntentlaunchActivity);
            }
            else if(v == Button02)
            {
            	IntentlaunchActivity = new Intent(MenuActivity.this, Press.class);
                startActivity(IntentlaunchActivity);
            }
            else if(v == Button03)
            {
            	IntentlaunchActivity = new Intent(MenuActivity.this, Form.class);
                startActivity(IntentlaunchActivity);
            }
            else if(v == Button04)
            {
            	IntentlaunchActivity = new Intent(MenuActivity.this, News.class);
                startActivity(IntentlaunchActivity);
                finish();
            }
        }   
}
 
  • Like
Reactions: sigurros
Upvote 0
Trying the code in emulator now.

I will be releasing it to market. Haven't done too much as far as research for how to do that but I know I have to sign it for that. Hope the process of publishing isn't as difficult as building it.

/bow to programmers and app writers. gotta say I'm loving learning this stuff.
 
Upvote 0
Success! Thank you so much.

Now I have to package it to test on phone for my boss and then publish it. If there's any place you know to learn this or tips please let me know. Thank you all, if there's someway to give you guys thanks (other than pressing the thanks button because I already did) let me know

=D

Sigurros
 
Upvote 0
Oh, woops.
This should fix it:

Sorry to be critical, but it might be an idea to try your code and make sure it at least compiles before posting it. Especially when trying to help a beginner - it can just cause more confusion. (I haven't tried to compile it, because I can see it's got errors in it.)

Mark

p.s. after submitting this comment 2 new posts appeared, and sigurros got it working. So I guess my comment is redundant.
 
Upvote 0
Sorry to be critical, but it might be an idea to try your code and make sure it at least compiles before posting it. Especially when trying to help a beginner - it can just cause more confusion. (I haven't tried to compile it, because I can see it's got errors in it.)

Mark

p.s. after submitting this comment 2 new posts appeared, and sigurros got it working. So I guess my comment is redundant.

I am not sure what errors you see in the second one as it is fine. The first post, however, I did make a couple newbie mistakes, but that is what I get for just typing it up, spur of the moment.

Also, How am I supposed to test it w/o making an entire layout file and supporting code?

If I went and did all of that, I might as well just call it my application and publish it myself. I was simply helping him with a method, not writing the program for him.
 
  • Like
Reactions: sigurros
Upvote 0
I am not sure what errors you see in the second one as it is fine.

Code:
. . .
        public void onClick(View v) {
            Intent intentLaunchActivity;
            
            if(v == Button01)
            {
            	IntentlaunchActivity = new Intent(MenuActivity.this, Clsdbanks.class);
            	startActivity(IntentlaunchActivity);
            }
. . .

"IntentLaunchActivity" rather than "intentLaunchActivity" was enough to tell me that the code wouldn't compile.

Also, How am I supposed to test it w/o making an entire layout file and supporting code?

You don't need to test it, or have an entire project to catch problems with a switch statement or undeclared variables. (There's no problem posting pseudo-code, or partial bits of code, as nobody will expect them to be complete or tested.)

If I went and did all of that, I might as well just call it my application and publish it myself.

Really?

Mark
 
Upvote 0
Code:
. . .
        public void onClick(View v) {
            Intent intentLaunchActivity;
            
            if(v == Button01)
            {
            	IntentlaunchActivity = new Intent(MenuActivity.this, Clsdbanks.class);
            	startActivity(IntentlaunchActivity);
            }
. . .

"IntentLaunchActivity" rather than "intentLaunchActivity" was enough to tell me that the code wouldn't compile.



You don't need to test it, or have an entire project to catch problems with a switch statement or undeclared variables. (There's no problem posting pseudo-code, or partial bits of code, as nobody will expect them to be complete or tested.)



Really?

Mark

Oh, I am sorry for the typos... Jeesh... Like I said, I just threw it out real quick. it was just to give him an idea of where to go with it. And looking back over the code, the 4 capital I's were the only mistakes in the code. Those are easily recognizable.
 
Upvote 0
Oh, I am sorry for the typos... Jeesh... Like I said, I just threw it out real quick. ...

Completely missing my point.

I only pointed out the undeclared variables (typos) because you thought your code was correct, and wanted to know what was wrong with it. I was making a more general point.

I started typing a longer response, but I realised I'd already spent enough time on this, and it would be pointless anyway. So that's me done for this thread. I'm sure I'll bump into you again in other threads.

Mark
 
Upvote 0
Completely missing my point.

I only pointed out the undeclared variables (typos) because you thought your code was correct, and wanted to know what was wrong with it. I was making a more general point.

I started typing a longer response, but I realised I'd already spent enough time on this, and it would be pointless anyway. So that's me done for this thread. I'm sure I'll bump into you again in other threads.

Mark

Eh.. Sorry for being a little vibrant... I am having a rough day at work and I was taking it out on you. I apologize. I realize that our misunderstanding was simply delays in reading posts.

No hard feelings.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones