Android Interface Webview
I want to create an interface for my project that has a button and when the button is clicked it begins the activity from another java file. The java file opens a webview. My first button works but the second one keeps force closing. Here is what I have:

Main program

        package com.dcu;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    public class DCU extends Activity
    {
        private Button newsbutton, calendarbutton;
    
        
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
    	    super.onCreate(savedInstanceState);
    	    setContentView(R.layout.main);
        	newsbutton = (Button)findViewById(R.id.News);
        	calendarbutton = (Button)findViewById(R.id.Calendar);
    
    
    	    //initControls();
        
        OnClickListener myListener = new OnClickListener()
        {
        	public void onClick(View v)
        	{
                if(v==newsbutton)
                {
            		Toast.makeText(DCU.this,"Loading News", Toast.LENGTH_SHORT).show();
            		Intent i = new Intent(DCU.this, News.class);
            		DCU.this.startActivity(i);            
            	}
                else if(v==calendarbutton)
                {
            		Toast.makeText(DCU.this,"Loading Calendar", Toast.LENGTH_SHORT).show();
            		Intent i = new Intent(DCU.this, Calendar.class);
            		DCU.this.startActivity(i);                
            	}
        	}
        };
    	newsbutton.setOnClickListener(myListener);
    	calendarbutton.setOnClickListener(myListener);
        }
    }


News program:

    package com.dcu;
    
    import java.io.IOException;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Window;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    public class News extends Activity {
    	WebView mWebView;
    	String test2 = "<html><body><table border='0'>";
    	Document docs;
    	Document writing;
    
    	String text(String link)
    	{
    		String full ="<html><body><table border='0'><tr><td align ='center'>";;
            try {
    			 writing = Jsoup.connect(link).get();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		Elements classname = writing.getElementsByClass("news");
    		Element heading = classname.select("h2").first();
    		Elements items = classname.select("p");
    
    		full = full + "<h1>" + heading.text()+ "</h1>" + "<h2>" + items.get(0).toString() + "</h1>" + "</td></tr>";
            Element imgs2 = writing.select("div.News img").first();
    
    		String picture = imgs2.absUrl("src");
    		String newImg = "<img src=\"" + picture + "\" width = '450' align = 'center'>";
    		full = full + "<tr><td align='center'>" + newImg + "</td></tr>";
    		full = full + "<tr><td><h1>";
    
    		for (int i = 1; i< items.size(); i++)
    		{
    			full = full + items.get(i).toString();
    		}
    		
    		full = full + "</h1></td></tr></table></body></html>";
    		return full;
    	}
    	public void main(String... args) 
    	{
            try 
            {
    			 docs = Jsoup.connect("http://www.dcu.ie/news/index.shtml").get();
    		} 
            catch (IOException e) 
            {
    			e.printStackTrace();
    		}
            Elements imgs = docs.select("div.news-feature img");
    
    		Elements txt = docs.select("h2");
    		
    		Elements article = docs.getElementsByClass("date");
    		Elements links = article.select("a[href]");
    
    		for (int i = 0; i < imgs.size(); i++){
    			String url = imgs.get(i).absUrl("src");
    			String temp = links.get(i).absUrl("href");
    
    			String temp2 = "<a href=\"" + temp + "\">";
    		    String newImg = temp2 + "<img src=\"" + url + "\" align = 'center' width = '300'>"+ "</a>";
    		    test2 = test2 + "<tr>";
    			  
    			test2 = test2 + "<td>";
    			test2 = test2 + " " + newImg + " ";
    			test2 = test2 + "</td>";
    			test2 = test2 + "<td>";
    			test2 = test2 + "<h1>" + txt.get(i).text() + "</h1>";
    			test2 = test2 + "</td>";
    			test2 = test2 + "</tr>";
    			
    			}
    		test2 = test2 + "</table>";
    		test2 = test2 + "</html></body>";
    		
    	}
    
        public void onCreate(Bundle savedInstanceState) {
        	main();
    
            super.onCreate(savedInstanceState);
            this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.webview );
             
            // Makes Progress bar Visible
            getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.setWebViewClient(new NewsClient());
            mWebView.setInitialScale(1);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setDomStorageEnabled(true);
            mWebView.loadData(test2, "text/html", "utf-8");
    
            final Activity MyActivity = this;
            mWebView.setWebChromeClient(new WebChromeClient() {
             public void onProgressChanged(WebView view, int progress)  
             {
              //Make the bar disappear after URL is loaded, and changes string to Loading...
              MyActivity.setTitle("Loading...");
              MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
             
              // Return the app name after finish loading
                 if(progress == 100)
                    MyActivity.setTitle(R.string.app_name);
               }
             });
            
            //mWebView.loadUrl("http://www.google.com");
            //mWebView.loadDataWithBaseURL("", test2,"text/html", "utf-8", "");
    
        }
    
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) 
            {
            	mWebView.goBack();        	
                return false;
            }
            return super.onKeyDown(keyCode, event);
        }
     
        private class NewsClient extends WebViewClient {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
            	if (view.canGoBack() == false)
            	{
    	        	String newUrl = text(url);
    		       	view.loadData(newUrl, "text/html", "utf-8");
            	}
            	else
            	{
            		view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            		view.loadUrl(url);
            	}
    
                //view.loadDataWithBaseURL("", newUrl,"text/html", "utf-8", "");
    
                return true;
            }
        }
    }


Calendar program:


        package com.dcu;
    
    import java.io.IOException;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Window;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    
    public class Calendar extends Activity {
    	WebView mWebView;
    	String html = "<html><body><table border='1'>";
    	Document docs;
    
    	public void main(String... args) {
    
            try {
    			 docs = Jsoup.connect("http://www.dcu.ie/timetables/ac.shtml").get();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    		Element table = docs.select("table").first();
    	    Elements tables = table.select("tr");
    
    		for (int i = 1; i < tables.size(); i ++) 
    		{
    			html += "<tr>" + tables.get(i).toString() + "</tr>";
    			/*while (tables.get(i).text() == "")
    			i++;
    			html += "<tr>" + tables.get(i).toString();
    			while (tables.get(i+1).text() == "")
    			i++;
    			html +=tables.get(i+1).toString() + "</tr>";
    			i++;*/
    		}
    			
    		html += "</table></body></html>";
    
    	}
    
        public void onCreate(Bundle savedInstanceState) {
        	main(); 
             
            super.onCreate(savedInstanceState);
            this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.webview);
            getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.setInitialScale(1);
            //mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setDomStorageEnabled(true);
            mWebView.loadData(html, "text/html", "utf-8");
            
            final Activity MyActivity = this;
            mWebView.setWebChromeClient(new WebChromeClient() {
             public void onProgressChanged(WebView view, int progress)  
             {
              //Make the bar disappear after URL is loaded, and changes string to Loading...
              MyActivity.setTitle("Loading...");
              MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
             
              // Return the app name after finish loading
                 if(progress == 100)
                    MyActivity.setTitle(R.string.app_name);
               }
             });
        }
    
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                mWebView.goBack();            
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
        
    
    }


Main.xml

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="news" android:id="@+id/News" android:text="@string/News"></Button>
    <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/Calendar" android:text="@string/Calendar" android:onClick="calendar"></Button>
    </LinearLayout>


webview.xml

        <?xml version="1.0" encoding="utf-8"?>
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />



Android Interface Webview
===

I want to create an interface for my project that has a button and when the button is clicked it begins the activity from another java file. The java file opens a webview. My first button works but the second one keeps force closing. Here is what I have:

Main program

        package com.dcu;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    public class DCU extends Activity
    {
        private Button newsbutton, calendarbutton;
    
        
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
    	    super.onCreate(savedInstanceState);
    	    setContentView(R.layout.main);
        	newsbutton = (Button)findViewById(R.id.News);
        	calendarbutton = (Button)findViewById(R.id.Calendar);
    
    
    	    //initControls();
        
        OnClickListener myListener = new OnClickListener()
        {
        	public void onClick(View v)
        	{
                if(v==newsbutton)
                {
            		Toast.makeText(DCU.this,"Loading News", Toast.LENGTH_SHORT).show();
            		Intent i = new Intent(DCU.this, News.class);
            		DCU.this.startActivity(i);            
            	}
                else if(v==calendarbutton)
                {
            		Toast.makeText(DCU.this,"Loading Calendar", Toast.LENGTH_SHORT).show();
            		Intent i = new Intent(DCU.this, Calendar.class);
            		DCU.this.startActivity(i);                
            	}
        	}
        };
    	newsbutton.setOnClickListener(myListener);
    	calendarbutton.setOnClickListener(myListener);
        }
    }


News program:

    package com.dcu;
    
    import java.io.IOException;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Window;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    public class News extends Activity {
    	WebView mWebView;
    	String test2 = "<html><body><table border='0'>";
    	Document docs;
    	Document writing;
    
    	String text(String link)
    	{
    		String full ="<html><body><table border='0'><tr><td align ='center'>";;
            try {
    			 writing = Jsoup.connect(link).get();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		Elements classname = writing.getElementsByClass("news");
    		Element heading = classname.select("h2").first();
    		Elements items = classname.select("p");
    
    		full = full + "<h1>" + heading.text()+ "</h1>" + "<h2>" + items.get(0).toString() + "</h1>" + "</td></tr>";
            Element imgs2 = writing.select("div.News img").first();
    
    		String picture = imgs2.absUrl("src");
    		String newImg = "<img src=\"" + picture + "\" width = '450' align = 'center'>";
    		full = full + "<tr><td align='center'>" + newImg + "</td></tr>";
    		full = full + "<tr><td><h1>";
    
    		for (int i = 1; i< items.size(); i++)
    		{
    			full = full + items.get(i).toString();
    		}
    		
    		full = full + "</h1></td></tr></table></body></html>";
    		return full;
    	}
    	public void main(String... args) 
    	{
            try 
            {
    			 docs = Jsoup.connect("http://www.dcu.ie/news/index.shtml").get();
    		} 
            catch (IOException e) 
            {
    			e.printStackTrace();
    		}
            Elements imgs = docs.select("div.news-feature img");
    
    		Elements txt = docs.select("h2");
    		
    		Elements article = docs.getElementsByClass("date");
    		Elements links = article.select("a[href]");
    
    		for (int i = 0; i < imgs.size(); i++){
    			String url = imgs.get(i).absUrl("src");
    			String temp = links.get(i).absUrl("href");
    
    			String temp2 = "<a href=\"" + temp + "\">";
    		    String newImg = temp2 + "<img src=\"" + url + "\" align = 'center' width = '300'>"+ "</a>";
    		    test2 = test2 + "<tr>";
    			  
    			test2 = test2 + "<td>";
    			test2 = test2 + " " + newImg + " ";
    			test2 = test2 + "</td>";
    			test2 = test2 + "<td>";
    			test2 = test2 + "<h1>" + txt.get(i).text() + "</h1>";
    			test2 = test2 + "</td>";
    			test2 = test2 + "</tr>";
    			
    			}
    		test2 = test2 + "</table>";
    		test2 = test2 + "</html></body>";
    		
    	}
    
        public void onCreate(Bundle savedInstanceState) {
        	main();
    
            super.onCreate(savedInstanceState);
            this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.webview );
             
            // Makes Progress bar Visible
            getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.setWebViewClient(new NewsClient());
            mWebView.setInitialScale(1);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setDomStorageEnabled(true);
            mWebView.loadData(test2, "text/html", "utf-8");
    
            final Activity MyActivity = this;
            mWebView.setWebChromeClient(new WebChromeClient() {
             public void onProgressChanged(WebView view, int progress)  
             {
              //Make the bar disappear after URL is loaded, and changes string to Loading...
              MyActivity.setTitle("Loading...");
              MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
             
              // Return the app name after finish loading
                 if(progress == 100)
                    MyActivity.setTitle(R.string.app_name);
               }
             });
            
            //mWebView.loadUrl("http://www.google.com");
            //mWebView.loadDataWithBaseURL("", test2,"text/html", "utf-8", "");
    
        }
    
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) 
            {
            	mWebView.goBack();        	
                return false;
            }
            return super.onKeyDown(keyCode, event);
        }
     
        private class NewsClient extends WebViewClient {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
            	if (view.canGoBack() == false)
            	{
    	        	String newUrl = text(url);
    		       	view.loadData(newUrl, "text/html", "utf-8");
            	}
            	else
            	{
            		view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            		view.loadUrl(url);
            	}
    
                //view.loadDataWithBaseURL("", newUrl,"text/html", "utf-8", "");
    
                return true;
            }
        }
    }


Calendar program:


        package com.dcu;
    
    import java.io.IOException;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Window;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    
    public class Calendar extends Activity {
    	WebView mWebView;
    	String html = "<html><body><table border='1'>";
    	Document docs;
    
    	public void main(String... args) {
    
            try {
    			 docs = Jsoup.connect("http://www.dcu.ie/timetables/ac.shtml").get();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    		Element table = docs.select("table").first();
    	    Elements tables = table.select("tr");
    
    		for (int i = 1; i < tables.size(); i ++) 
    		{
    			html += "<tr>" + tables.get(i).toString() + "</tr>";
    			/*while (tables.get(i).text() == "")
    			i++;
    			html += "<tr>" + tables.get(i).toString();
    			while (tables.get(i+1).text() == "")
    			i++;
    			html +=tables.get(i+1).toString() + "</tr>";
    			i++;*/
    		}
    			
    		html += "</table></body></html>";
    
    	}
    
        public void onCreate(Bundle savedInstanceState) {
        	main(); 
             
            super.onCreate(savedInstanceState);
            this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.webview);
            getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.setInitialScale(1);
            //mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setDomStorageEnabled(true);
            mWebView.loadData(html, "text/html", "utf-8");
            
            final Activity MyActivity = this;
            mWebView.setWebChromeClient(new WebChromeClient() {
             public void onProgressChanged(WebView view, int progress)  
             {
              //Make the bar disappear after URL is loaded, and changes string to Loading...
              MyActivity.setTitle("Loading...");
              MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
             
              // Return the app name after finish loading
                 if(progress == 100)
                    MyActivity.setTitle(R.string.app_name);
               }
             });
        }
    
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                mWebView.goBack();            
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
        
    
    }


Main.xml

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="news" android:id="@+id/News" android:text="@string/News"></Button>
    <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/Calendar" android:text="@string/Calendar" android:onClick="calendar"></Button>
    </LinearLayout>


webview.xml

        <?xml version="1.0" encoding="utf-8"?>
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />


