1
嗨,我是android新手。Android主要活動有2個按鈕在webview中打開第二個活動
我的主要活動有2個按鈕。如果用戶點擊按鈕1,它將在webview中打開谷歌網站,按鈕2在webview中打開雅虎網站。我不想在任何其他瀏覽器中打開。
這是我的代碼;但它不工作,任何人都可以告訴我,我在這裏做錯了什麼。
package com.jo.mavselect;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class SelectRm extends Activity {
public String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_rm);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.select_rm, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called when the user clicks the Send button */
public void sendMessage(View v) {
if(v.getId()==R.id.B9)
{
message ="www.google.com.au";
}
else
if(v.getId()==R.id.B10)
{
message ="www.google.com.au";
}
Intent intent = new Intent(this, MavisActivity.class);
intent.putExtra("msg",message);
startActivity(intent);
}
}
** ** MavisActivity
package com.jo.mavselect;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
public class MavisActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
setContentView(mWebView);
mWebView =(WebView) findViewById(R.id.Webview);
mWebView.setWebViewClient(new WebViewClient());
// web page to fit to the screen
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
// Not to cache
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
// Enable Java script
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// No to cache
mWebView.clearCache(true);
//Clears any saved data for web forms.
mWebView.clearFormData();
//Tells this WebView to clear its internal back/forward list.
mWebView.clearHistory();
Intent intent = getIntent();
String ur = intent.getExtras().getString("msg");
mWebView.loadUrl(ur);
final Activity activity = this;
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/error.html");
}
});
}
}
清單文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jo.mavselect" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jo.mavselect.MavisActivity"
android:label="MavisActivity"
android:parentActivityName="com.jo.mavselect.SelectRm" >
// android:label="@string/app_name" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.jo.mavselect.SelectRm" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
活動xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".SelectRm/">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goole"
android:id="@+id/B9"
android:layout_marginTop="60dp"
android:layout_alignParentTop="true"
android:onClick="sendMessage" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yahoo"
android:id="@+id/B10"
android:layout_toEndOf="@+id/B9"
android:layout_marginLeft="84dp"
android:layout_alignTop="@+id/B9"
android:layout_toRightOf="@+id/B9"
android:onClick="sendMessage" />
<WebView
android:id="@+id/Webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
你不需要這個'mWebView =(的WebView)findViewById(R.id.Webview);' – Raghunandan 2014-09-13 14:32:57