2011-03-23 87 views
0

我要創建我的項目有一個按鈕,單擊該按鈕時,它從另一個Java文件,該文件是一個庫,但我有一些困難,開始活動的接口。引用的Java文件打開網頁視圖。以下是我有Android界面網頁視圖

package com.dcu; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import com.news.*; 

public class DCU extends Activity 
{ 
    private Button somebutton; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     initControls(); 
    } 
    protected void initControls() 
    { 
     somebutton = (Button)findViewById(R.id.News); 
     somebutton.setOnClickListener(new Button.OnClickListener() 
     { 
      public void onClick (View v) 
      { 
       News n = new News(); 
       n.onCreate(null); 
      } 
     }); 
} 

}

然後,我有一個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:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="news" android:id="@+id/News"></Button> 
</LinearLayout> 

而且news.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" 
/> 

這裏是News.java

package com.news; 

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; 
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); 
     //setContentView(R.layout.main); 
     this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     setContentView(R.layout.main); 

     // 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; 
     } 
    } 
} 

謝謝

回答

0

使用意圖開始您的活動。相反的:

News n = new News(); 
n.onCreate(null); 

用途:

Intent i = new Intent(DCU.this, News.class); 
DCU.this.startActivity(i); 
+0

感謝您的,但是當我按一下按鈕我仍然獲得了強制關閉的消息 – dbaby7 2011-03-23 14:39:34