简体   繁体   中英

Load different website urls in different activities using Android WebView

I am new to Android app development and so far I have been able to develop a prototype that displays one url. I trued adding another webview which is set to load a different URLs but whenever I open the second activity, the application shuts down. Is it possible to achieve this and if so, HOW? Thanks in advance.

Here is the sample code in my android application.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Activity_Two"
        android:label="@string/title_activity_activity__two" >
    </activity>
    <activity
        android:name=".ActivityThree"
        android:label="@string/title_activity_activity_three" >
    </activity>
</application>

MainActivity.java

public class MainActivity extends ActionBarActivity {
private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.main_webview);

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.loadUrl("http://test.biko.me.ke");

    // Stop local links and redirects from opening in browser instead of WebView
    mWebView.setWebViewClient(new MyAppWebViewClient());
}

Activity_Two.java

public class Activity_Two extends ActionBarActivity {
private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.two_webview);

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.loadUrl("http://biko.me.ke");

    // Stop local links and redirects from opening in browser instead of WebView
    mWebView.setWebViewClient(new MyAppWebViewClient());
}

I think problem is in AndroidManifest.xml file.You have to register 2nd activity in AndroidManifest.xml file. After that you can move to 2nd activity without any crash. Here is an example. First activity is main launcher activity and 2nd is other activity

    <activity
        android:name="com.tenmb.androidexpert.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.tenmb.androidexpert.Beginners.ButtonExample"
        android:label="@string/app_name">
    </activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM