eclipse中的Android应用程序
我已经找了好几天了,但找不到答案,也许你们能帮我 我在eclipse中创建了一个android应用程序,一切正常,但有一件事困扰着我 这是我的main.java:eclipse中的Android应用程序,android,eclipse,Android,Eclipse,我已经找了好几天了,但找不到答案,也许你们能帮我 我在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.OnClickL
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页面中创建的按钮。有人知道这是为什么吗
非常感谢你的帮助
哦,这是我的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>
看起来您正在创建第二个网络视图,然后将其设置为内容视图,因此您的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);
实际上是:
this.webview = (WebView) findViewById(R.**id**.webview);
编辑2:
我在做这个项目的时候注意到了另一件事。webscreen.xml中的以下内容:
android:id="@string/webview"
android:layout_height="0dip"
应该是:
android:id="@+id/webview"
编辑3:
在webscreen.xml中还注意到另一件事:
android:id="@string/webview"
android:layout_height="0dip"
确实要0高度???;-) 我想我们需要更多的信息。例如,您的Webscreen活动的布局XML文件是什么?感谢您的快速回复,非常感谢,我在我的帖子中添加了布局代码;setContentView(网络视图);按钮和文本栏已加载,但页面不再加载,希望这有助于在您回复时发布此消息。好的,以上就是为什么按钮不显示的原因,因为您已经替换了视图。不幸的是,我不熟悉WebView的工作原理,因此我无法解释为什么页面不加载。好的,但是如果我删除这些行,它将不会显示WebView。对如何解决这个问题有什么建议吗?你能告诉我正确的方向吗?刚刚编辑了我的原始答案。如果这不是解决方案,我建议您在此处查看WebView教程:我将其更改为this.WebView=(WebView)findViewById(R.id.WebView);但是没有区别。我只是把你的代码复制到一个项目中,如果我能重新创建并修复解决方案,我会在短时间内发回。