Bubble07
Bubble07

Reputation: 13

Android WebView returns weird error

I am trying to create a simple browser app. Here is my code:

package com.degstu.ultralightbrowser;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

private Button button;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

//Code for "GO" button
public void sendURL(View view) {
    TextView textURL;
    textURL = (TextView) findViewById(R.id.textBoxURL);

    WebView webView = new WebView(this);
    webView.loadUrl(textURL.toString());
    setContentView(webView);

}

}

Everything mostly works, however, when I press "GO" in the app, I see this. There are no errors recognized in my code, and any help would be appreciated.

Upvotes: 0

Views: 68

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

First of all,avoid extends ActionBarActivity .Use Activity or AppCompatActivity.

WHY

Since the version 22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity ORActivity .

Please update your Code Like this

        TextView textURL;
        textURL = (TextView) findViewById(R.id.textBoxURL);
        WebView webView = new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(textURL.getText().toString());

For details you can visit here Android - WebView Tutorial and https://developer.android.com/reference/android/webkit/WebView.html

I hope it helps you.

Upvotes: 1

rookiedev
rookiedev

Reputation: 1127

Apparently, the url you wanted to visit is not valid (it starts with "andorid.support.v7..." which is a string containing a concise, human-readable description of the TextView object). To get the text of TextView, you should use getText() to return the text the TextView is displaying.

webView.loadUrl(textURL.getText().toString());

Upvotes: 1

Related Questions