Button direct to webpage wont work.


Last Updated:

  1. sigurros

    sigurros Active Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    27
    Likes Received:
    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 (Text):
    1. package android.bankclosures;
    2.  
    3. import android.app.Activity;
    4. import android.content.Intent;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.webkit.WebView;
    8. import android.widget.Button;
    9.  
    10.  
    11. public class MenuActivity extends Activity {
    12.     WebView mWebView;
    13.     WebView mWebView1;
    14.     WebView mWebView2;
    15.     WebView mWebView3;
    16.  
    17.  
    18.     /** Called when the activity is first created. */
    19.     @Override
    20.     public void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.menu);
    23.        
    24.        Button Button01=(Button)findViewById(R.id.Button01);
    25.        Button01.setOnClickListener(new View.OnClickListener() {
    26.  
    27.         @Override
    28.         public void onClick(View v) {
    29.             Intent IntentlaunchActivity = new Intent(MenuActivity.this, Clsdbanks.class);
    30.             startActivity(IntentlaunchActivity);
    31.            
    32.            
    33.         Button Button02=(Button)findViewById(R.id.Button02);
    34.         Button02.setOnClickListener(new View.OnClickListener() {
    35.            
    36.             @Override
    37.             public void onClick(View v) {
    38.                 Intent IntentlaunchActivity1 = new Intent(MenuActivity.this, Press.class);
    39.                 startActivity(IntentlaunchActivity1);
    40.                
    41.         Button Button03=(Button)findViewById(R.id.Button03);
    42.         Button03.setOnClickListener(new View.OnClickListener() {
    43.                    
    44.             @Override
    45.             public void onClick(View v) {
    46.                 Intent IntentlaunchActivity2 = new Intent(MenuActivity.this, Form.class);
    47.                 startActivity(IntentlaunchActivity2);
    48.                        
    49.                
    50.         Button Button04=(Button)findViewById(R.id.Button04);
    51.         Button04.setOnClickListener(new View.OnClickListener() {
    52.                    
    53.             @Override
    54.             public void onClick(View v) {
    55.                 Intent IntentlaunchActivity3 = new Intent(MenuActivity.this, News.class);
    56.                 startActivity(IntentlaunchActivity3);
    57.                     finish();
    58.                     }
    59.                     });
    60.                
    61.             }
    62.         });
    63.         }
    64.  
    65.        
    66.              
    67.        
    68.  
    69.            
    70.        
    71.     });
    72.        
    73.  
    74.    
    75.        
    76.  
    77.     }
    78.  
    79.        
    80.     });
    81.     }
    82. }
    83.  
    84.  
    85.  


    Working Activity Java

    Code (Text):
    1. package android.bankclosures;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.KeyEvent;
    6. import android.webkit.WebView;
    7. import android.webkit.WebViewClient;
    8.  
    9. public class Clsdbanks extends Activity {
    10.     WebView mWebView;
    11.  
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.clsdbanks);
    15.        
    16.         mWebView = (WebView) findViewById(R.id.webview);
    17.         mWebView.getSettings().setJavaScriptEnabled(true);
    18.         mWebView.loadUrl("http://www.fdic.gov/bank/individual/failed/banklist.html");
    19.        
    20.  class ClsdbanksClient extends WebViewClient {
    21.               @Override
    22.               public boolean shouldOverrideUrlLoading(WebView view, String url) {
    23.                   view.loadUrl(url);
    24.                   return true;
    25.               }
    26.           }
    27.           mWebView.setWebViewClient(new ClsdbanksClient());
    28.     }
    29.           @Override
    30.           public boolean onKeyDown(int keyCode, KeyEvent event) {
    31.               if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
    32.                   mWebView.goBack();
    33.                   return true;
    34.           }
    35.           return super.onKeyDown(keyCode, event);
    36.     }
    37.          
    38.     }
    39.  


    Non Working JAva

    Code (Text):
    1. package android.bankclosures;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.KeyEvent;
    6. import android.webkit.WebView;
    7. import android.webkit.WebViewClient;
    8.  
    9. public class Press extends Activity {
    10.     WebView mWebView1;
    11.  
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.press);
    15.        
    16.         mWebView1 = (WebView) findViewById(R.id.webview);
    17.         mWebView1.getSettings().setJavaScriptEnabled(true);
    18.         mWebView1.loadUrl("http://www.fdic.gov/bank/historical/bank/index.html");
    19.        
    20.  class PressClient extends WebViewClient {
    21.               @Override
    22.               public boolean shouldOverrideUrlLoading(WebView view, String url) {
    23.                   view.loadUrl(url);
    24.                   return true;
    25.               }
    26.           }
    27.           mWebView1.setWebViewClient(new PressClient());
    28.     }
    29.           @Override
    30.           public boolean onKeyDown(int keyCode, KeyEvent event) {
    31.               if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView1.canGoBack()) {
    32.                   mWebView1.goBack();
    33.                   return true;
    34.           }
    35.           return super.onKeyDown(keyCode, event);
    36.     }
    37.          
    38.     }
    39.  


    MAnifest

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.       package="android.bankclosures"
    4.       android:versionCode="1"
    5.       android:versionName="1.0">
    6.       <uses-permission android:name="android.permission.INTERNET" />
    7.     <application android:label="@string/app_name" android:debuggable="true" android:enabled="true" android:icon="@drawable/logonobg">
    8.        
    9.  
    10.     <activity android:name=".SplashActivity" android:label="@string/app_name">
    11.             <intent-filter>
    12.                 <action android:name="android.intent.action.MAIN"/>
    13.                 <category android:name="android.intent.category.LAUNCHER" />
    14.             </intent-filter>
    15.        
    16. </activity><activity android:name=".MenuActivity" android:label="Menu">
    17. </activity>
    18.  
    19.  
    20. <activity android:label="Closed Banks" android:name=".Clsdbanks"></activity>
    21. <activity android:label="Press" android:name=".Press"></activity>
    22. <activity android:name=".Form" android:label="Help Form"></activity>
    23. <activity android:name=".News" android:label="RK News"></activity>
    24. </application>
    25.     <uses-sdk android:minSdkVersion="8" />
    26.  
    27.  
    28. </manifest>
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Change your MenuActivity to this:

    Code (Text):
    1.  
    2. public class MenuActivity extends Activity implements OnClickListener{
    3.     WebView mWebView;
    4.     WebView mWebView1;
    5.     WebView mWebView2;
    6.     WebView mWebView3;
    7.  
    8.  
    9.     /** Called when the activity is first created. */
    10.     @Override
    11.     public void onCreate(Bundle savedInstanceState) {
    12.         super.onCreate(savedInstanceState);
    13.         setContentView(R.layout.menu);
    14.        
    15.        Button Button01=(Button)findViewById(R.id.Button01);
    16.        Button Button02=(Button)findViewById(R.id.Button02);
    17.        Button Button03=(Button)findViewById(R.id.Button03);
    18.        Button Button04=(Button)findViewById(R.id.Button04);
    19.        Button01.setOnClickListener(this);
    20.        Button02.setOnClickListener(this);
    21.        Button03.setOnClickListener(this);
    22.        Button04.setOnClickListener(this);
    23.     }
    24.                
    25.        
    26.         public void onClick(View v) {
    27.             Intent intentLaunchActivity;
    28.             switch(v)
    29.             {
    30.             case Button01:
    31.                 IntentlaunchActivity = new Intent(MenuActivity.this, Clsdbanks.class);
    32.                 startActivity(IntentlaunchActivity);
    33.                 break;
    34.             case Button02:
    35.                 IntentlaunchActivity = new Intent(MenuActivity.this, Press.class);
    36.                 startActivity(IntentlaunchActivity);
    37.                 break;
    38.             case Button03:
    39.                 IntentlaunchActivity = new Intent(MenuActivity.this, Form.class);
    40.                 startActivity(IntentlaunchActivity);
    41.                 break;
    42.             case Button04:
    43.                 IntentlaunchActivity = new Intent(MenuActivity.this, News.class);
    44.                 startActivity(IntentlaunchActivity);
    45.                 finish();
    46.                 break;
    47.             }
    48.         }  
    49. }
    50.  
    EDIT: It seems like it should have worked the way you had it, but try it this way just to be safe.
     
    sigurros likes this.
  3. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Ah wait, in your manifest change the activity definitions to:

    Code (Text):
    1.  
    2. <activity android:label="Closed Banks" android:name=".Clsdbanks"></activity>
    3. <activity android:label=".Press" android:name=".Press"></activity>
    4. <activity android:name=".Form" android:label="Help Form"></activity>
    5. <activity android:name=".News" android:label="RK News"></activity>
    6.  
    You forgot the "." in the Press activity's name property.
     
    sigurros likes this.
  4. sigurros

    sigurros Active Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    27
    Likes Received:
    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.
     
  5. maest

    maest Member

    Joined:
    Aug 5, 2010
    Messages:
    23
    Likes Received:
    2
    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.
     
    sigurros likes this.
  6. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    What errors are you getting using my code? Using my code should fix the cascading errors if you can get it to work.
     
    sigurros likes this.
  7. maest

    maest Member

    Joined:
    Aug 5, 2010
    Messages:
    23
    Likes Received:
    2
    I should point out that the compiler complained about trying to do a switch statement over a View object.
     
    sigurros likes this.
  8. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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
     
    sigurros likes this.
  9. sigurros

    sigurros Active Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    27
    Likes Received:
    0
    Cannot switch on a value of type View. Only convertible int values or enum constants are permitted - for the code

    Code (Text):
    1.  switch (v)
    Button04 cannot be resolved to a variable - this for each button.

    Code (Text):
    1. case Button04:
    might be a quick fix but I don't know the problem.
     
  10. sigurros

    sigurros Active Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    27
    Likes Received:
    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.
     
  11. sigurros

    sigurros Active Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    27
    Likes Received:
    0
    Ah I see that they are put inside one another in that view. Blah I should have figured that.
     
  12. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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
     
    sigurros likes this.
  13. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Oh, woops.
    This should fix it:

    Code (Text):
    1.  
    2. public class MenuActivity extends Activity implements OnClickListener{
    3.     WebView mWebView;
    4.     WebView mWebView1;
    5.     WebView mWebView2;
    6.     WebView mWebView3;
    7.     Button Button01;
    8.     Button Button02;
    9.     Button Button03;
    10.     Button Button04;
    11.  
    12.     /** Called when the activity is first created. */
    13.     @Override
    14.     public void onCreate(Bundle savedInstanceState) {
    15.         super.onCreate(savedInstanceState);
    16.         setContentView(R.layout.menu);
    17.        
    18.        Button01=(Button)findViewById(R.id.Button01);
    19.        Button02=(Button)findViewById(R.id.Button02);
    20.        Button03=(Button)findViewById(R.id.Button03);
    21.        Button04=(Button)findViewById(R.id.Button04);
    22.        Button01.setOnClickListener(this);
    23.        Button02.setOnClickListener(this);
    24.        Button03.setOnClickListener(this);
    25.        Button04.setOnClickListener(this);
    26.     }
    27.                
    28.        
    29.         public void onClick(View v) {
    30.             Intent intentLaunchActivity;
    31.            
    32.             if(v == Button01)
    33.             {
    34.                 IntentlaunchActivity = new Intent(MenuActivity.this, Clsdbanks.class);
    35.                 startActivity(IntentlaunchActivity);
    36.             }
    37.             else if(v == Button02)
    38.             {
    39.                 IntentlaunchActivity = new Intent(MenuActivity.this, Press.class);
    40.                 startActivity(IntentlaunchActivity);
    41.             }
    42.             else if(v == Button03)
    43.             {
    44.                 IntentlaunchActivity = new Intent(MenuActivity.this, Form.class);
    45.                 startActivity(IntentlaunchActivity);
    46.             }
    47.             else if(v == Button04)
    48.             {
    49.                 IntentlaunchActivity = new Intent(MenuActivity.this, News.class);
    50.                 startActivity(IntentlaunchActivity);
    51.                 finish();
    52.             }
    53.         }  
    54. }
    55.  
     
    sigurros likes this.
  14. sigurros

    sigurros Active Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    27
    Likes Received:
    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.
     
  15. sigurros

    sigurros Active Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    27
    Likes Received:
    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
     
  16. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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.
     
  17. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.
     
    sigurros likes this.
  18. sigurros

    sigurros Active Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    27
    Likes Received:
    0
    No complaints here, even if there was a mistake it was fixed FOR me pretty quickly. I cant thank you guys enough.
     
  19. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Code (Text):
    1.  
    2. . . .
    3.         public void onClick(View v) {
    4.             Intent intentLaunchActivity;
    5.            
    6.             if(v == Button01)
    7.             {
    8.                 IntentlaunchActivity = new Intent(MenuActivity.this, Clsdbanks.class);
    9.                 startActivity(IntentlaunchActivity);
    10.             }
    11. . . .
    12.  
    "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
     
  20. sigurros

    sigurros Active Member This Topic's Starter

    Joined:
    Jul 22, 2010
    Messages:
    27
    Likes Received:
    0
    :p

    seriously, the application is finished now and it was with thanks to all that helped. Closed thread, finished dramatics.
     
  21. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    And as you see, I fixed that code and sent him a working bit in the post you quoted...
     
  22. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.
     
  23. maest

    maest Member

    Joined:
    Aug 5, 2010
    Messages:
    23
    Likes Received:
    2
    Huh? I'm not sure what you're talking about. The error was there when I posted this. I'm not denying that the new version is working, it's just that the new version was posted after my post.
     
  24. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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
     
  25. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.
     

Share This Page

Loading...