2011-01-10 30 views
0

我已經搜索了幾天,但無法找到答案,也許你們可以幫忙。在eclipse中的Android應用程序

我在Eclipse中創建一個Android應用程序,這一切工作的只有一件事是竊聽我。

這是我main.java:

package com.test; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Toast; 

public class Main extends Activity implements OnClickListener { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

    // Add Click listeners for all buttons 
     View firstButton = findViewById(R.id.btn_rassen); 
     firstButton.setOnClickListener(this); 
     View secondButton = findViewById(R.id.button2); 
     secondButton.setOnClickListener(this); 
    } 

    // Process the button click events 
@Override 
public void onClick(View v) { 
    switch(v.getId()){ 
    case R.id.btn_rassen: 
    Intent j = new Intent(this, Webscreen.class); 
     j.putExtra(com.test.Webscreen.URL, 
     "http://www.google.com/"); 

     startActivity(j); 

    break; 

    case R.id.button2: 
    Intent k = new Intent(this, Webscreen.class); 
     k.putExtra(com.test.Webscreen.URL, 
     "http://notworkingurltotest.com"); 
     startActivity(k); 
    break; 

    } 
} 
} 

現在,當它調用webview.java稱爲頁面顯示出來,但不是我在佈局XML頁面創建的按鈕。有沒有人知道這是爲什麼?

您的幫助深表感謝!

OHW這是我webscreen.java

package com.test; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Window; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class Webscreen extends Activity { 

    public static final String URL = ""; 
    private static final String TAG = "WebscreenClass"; 
    private WebView webview; 
    private ProgressDialog progressDialog; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.webscreen); 

     this.getIntent().getExtras(); 
     this.webview = (WebView) findViewById(R.string.webview); 


     String turl = getIntent().getStringExtra(URL); 
     Log.i(TAG, " URL = "+turl); 

    WebView webview = new WebView(this); 
    setContentView(webview); 


     final Activity activity = this;  

     webview.setWebViewClient(new WebViewClient() { 
      public boolean shouldOverrideUrlLoading(WebView view, String url) {    
       view.loadUrl(url); 
       return true; 
      } 


      public void onLoadResource (WebView view, String url) { 
       if (progressDialog == null) { 
        progressDialog = new ProgressDialog(activity); 
        progressDialog.setMessage("Bezig met laden..."); 
        progressDialog.show(); 

       } 
      } 

      public void onPageFinished(WebView view, String url) { 
       if (progressDialog.isShowing()) { 
        progressDialog.dismiss(); 
        progressDialog = null; 


       } 
      } 


      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 



       Intent myIntent = new Intent(); 
       myIntent.setClassName("com.test", "com.test.Main"); 
       startActivity(myIntent); 

       Toast.makeText(activity, "Laden van onderdeel mislukt, probeer het later nog eens! ", Toast.LENGTH_LONG).show(); 

       progressDialog.show(); 
      } 

      }); 




     webview.loadUrl(turl); 

    } 
} 

webscreen.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"> 

    <!-- <1> --> 
    <LinearLayout android:orientation="horizontal" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"> 

    <EditText android:id="@+id/url" android:layout_height="wrap_content" 
     android:layout_width="wrap_content" android:lines="1" 
     android:layout_weight="1.0" android:hint="http://" 
     android:visibility="visible" /> 

    <Button android:id="@+id/go_button" android:layout_height="wrap_content" 
     android:layout_width="wrap_content" android:text="go_button" /> 

    </LinearLayout> 

    <!-- <2> --> 
    <WebView 
     android:id="@string/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 

     /> 
</LinearLayout> 
+0

我想我們需要更多的信息。例如什麼是您的Webscreen Activity的佈局XML文件? – C0deAttack 2011-01-10 21:43:50

+0

感謝您的快速回復C0deAttack,非常感謝,我添加了佈局代碼到我的文章。 – Colin 2011-01-10 21:50:37

回答

1

看起來像你創建第二個網頁視圖,然後設置,作爲內容視圖,讓您的R.layout.webscreen被替換。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.webscreen); 

    this.getIntent().getExtras(); 
    this.webview = (WebView) findViewById(R.string.webview); 

    String turl = getIntent().getStringExtra(URL); 
    Log.i(TAG, " URL = "+turl); 

    **WebView webview = new WebView(this); 
    setContentView(webview);** 
..... 

編輯:

我剛在你的代碼發現了一些,應該讀取的行:

this.webview = (WebView) findViewById(R.string.webview); 

NOTE:事實上是:

this.webview = (WebView) findViewById(R.**id**.webview); 

編輯2: 我在製作項目時剛注意到另一件事。在webscreen.xml以下:

android:id="@string/webview" 

應該是:

android:id="@+id/webview" 

編輯3: 而在webscreen.xml另一件事注意到:

android:layout_height="0dip" 

確定要一個0高度?? ;-)

相關問題