2011-01-10 72 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高度?? ;-)

相关问题