2011-01-10 6 views
0

Я искал дни, но не могу найти ответ, возможно, вы, ребята, можете помочь.Приложение для 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

Думаю, нам нужна дополнительная информация. Например, каков XML-файл макета для вашей активности в Webscreen? – C0deAttack

+0

спасибо за быстрый ответ C0deAttack, очень ценю, я добавил код макета на свой пост. – Colin

+0

если я прокомментирую: WebView webview = новый WebView (this); setContentView (webview); Загрузка кнопки и текстовой панели, но страница больше не загружается, надеюсь, что это поможет – Colin

ответ

1

Похоже, вы создаете второй WebView, а затем установив, что как вид контента, так что ваши 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);** 
..... 

Edit:

Я просто заметил, что-то в вашем коде, если линия, которая гласит:

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

Acutally быть:

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

Edit 2: Просто заметил еще одну вещь, когда я делал проект. Следующее в webscreen.xml:

android:id="@string/webview" 

Должно быть:

android:id="@+id/webview" 

Edit 3: И еще заметил в webscreen.xml:

android:layout_height="0dip" 

уверены, что хотите 0 высоту ?? ;-)

+0

Отправлено, пока вы ответили. Итак, вышесказанное, почему вы не показываете кнопки, потому что вы заменили представление. К сожалению, я не знаком с тем, как работает WebView, поэтому я не могу помочь, почему страница не загружается. – C0deAttack

+0

Хорошо, но если я удалю эти строки, он не покажет веб-просмотр. любые предложения о том, как это решить? Не могли бы вы указать мне в правильном направлении, пожалуйста? – Colin

+0

Просто отредактировал мой оригинальный ответ. Если это не решение, то все, что я могу предложить, это проверить учебник WebView здесь: http://developer.android.com/resources/tutorials/views/hello-webview.html – C0deAttack

Смежные вопросы