I am trying out a very simple webview application on Android. While the program builds OK, there are run-time errors that I cannot decode. The layout:
<?xml version="1.0" encoding="utf-8" ?>
<Webview xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/helloWebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
The code:
/* Program to create sample webview.
* Steps:
* 1. Create webview.
* 2. Show some website in it.
* 3. Show some transitions as well.
*/
package com.sriram.hellowebview;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;
public class helloWebview extends Activity {
WebView myWebview;
String url = "http://www.google.com";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_webview);
Log.v(this.toString(), "Starting activity.");
myWebview = (WebView) findViewById(R.id.helloWebview);
Log.v(this.toString(), "Getting settings.");
//myWebview.getSettings().setJavaScriptEnabled(true);
Log.v(this.toString(), "Loading URL now.");
myWebview.loadUrl(url);
Log.v(this.toString(), "Loaded URL.");
//open all links within the same webview.
//myWebview.setWebViewClient(new WebViewClient());
//Log.v(this.toString(), "All done here.");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hello_webview, menu);
return true;
}
}
The error observed:
03-24 14:48:27.618: E/AndroidRuntime(388): Uncaught handler: thread main exiting due to uncaught exception
03-24 14:48:27.658: E/AndroidRuntime(388): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sriram.hellowebview/com.sriram.hellowebview.helloWebview}: android.view.InflateException: Binary XML file line #3: Error inflating class Webview
I have come across similar errors that detail OutOfMemoryExceptions in large projects, but since the above code is all there is to the project, this does not seem a likely explanation.
There is typo mistake in your layout: WebView need to be written in CamelCase. There isn't such view as 'Webview'. So your layout should look as:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/helloWebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
You are receiving this error as you are not having any parent layout for webview.
Change your xml file as:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<WebView
android:id="#+id/helloWebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Hope it helps.
Related
I'm having problems with a webview inside a fragment. I'm getting a error message saying "java.lang.nullpointerexception" at this line: myWebView.setWebViewClient(new WebViewClient());. I have googled and googled but not found a solution. Any ideas as to what I'm missing?
FragmentB.java
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* A simple {#link Fragment} subclass.
*
*/
#SuppressLint("SetJavaScriptEnabled")
public class FragmentB extends Fragment {
public FragmentB() {
// Required empty public constructor
}
private View mContentView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mContentView = inflater.inflate(R.layout.fragment_b, container, false);
WebView myWebView = (WebView)mContentView. findViewById(R.id.webView);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.nellienova.com");
return mContentView;
}
}
webview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Make sure you are loading the right xml file. I have done this before by mistakenly loading another layout file. You have webview.xml typed above that file but you inflating R.layout.fragment_b
Edit
Each fragment has its own layout file. You can reuse layouts and add multiple layouts to one xml file but through code your fragment will only inflate the file you refer to.
If you want to include other files you have to use include statements to pull those other files in to your xml before you can interact with objects in the other xml files. Click here for more details about include statements. Example below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
</LinearLayout>
Click here for some good information about fragments. Spend a little time with it and it will save you lots of time troubleshooting problems.
This is my main.xml file.
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MainActivity"
/>
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/urlContainer" />
</LinearLayout>
And this is my java file.
package szdf.asdf;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity
{
private WebView webView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
String customHtml = "<html><body><h1>Hello, WebView</h1>" +
"<h1>Heading 1</h1><p>This is a sample paragraph.</p>" +
"</body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");
}
}
I am trying to add a webview to my app. But it compiles with the error
error: Error: No resource found that matches the given name (at 'layout_below' with value '#id/urlContainer').
Can someone tell me what is wrong with my code ?
android:layout_below="#id/urlContainer" />
You haven't resource with this ID, just delete it :)
You're referencing an object with an ID that doesn't exist on the android:layout_below attribute. Remove the line or add a item with the ID #id/urlContainer.
There is no resource container with this id urlContainer. SO remove this line:
android:layout_below="#id/urlContainer" />
There is a error in mView, I need solution
package com.example.account;
import android.app.Activity;
import android.os.Bundle;
public class WebView extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
My xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Cannot cast from view to webview
WebView mView = (WebView) findViewById(R.id.webView1)
Try this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
give me feedback on this.
Update:
You should change your Activity name with Different one. or defined your WebView with different name like:
WebView mView2 = (WebView) findViewById(R.id.webView1)
You are using your activity name as WebView. This name is already used by SDK API. This is the reason it is giving you error.
To solve it, just rename your WebView.java file to some another name like MyWebView.java then your problem will surely solve.
To safely rename your .java file, Just go to packageexplorer , select WebView.java and press F2, and give new name.
Try this, it may help
1 . Using Eclipse, create a new Android project and name it as WebView.
2 . Add the following statements to the main.xml file:
<?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” >
<WebView
android:id=”#+id/webview1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</LinearLayout>
3 . In the MainActivity.java file, add the following statements in bold:
package com.emergingandroidtech.WebView;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends Activity
{
/**Called when the activity is first created.*/
#Override
public void onCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wv = (WebView) findViewById(R.id.webview1);
WebSettings webSettings = wv.getSettings();
webSettings.setBuiltInZoomControls(true);
wv.loadUrl( “www.google.com”);
}
}
4. Don't forget to give the internet permission in manifest file
Thank you
You should keep webview inside any parent view
Do not use reserved words as your variable, class and method name.
Here, WebView is reserved or already used by SDK. If you use that word again, Compiler will get confused.
activity_main.xml
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity
{
private WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(WebView)findViewById(R.id.webview);
//loads androidride homepage to webview
webview.loadUrl("https://www.androidride.com");
}
}
When ever I try to use a web view it gives me a null pointer exception, my code:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class Main extends Activity {
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/www/index.html");
}
}
the Logs are:
02-18 22:08:06.224: ERROR/AndroidRuntime(355): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.web/com.test.web.Main}: java.lang.NullPointerException
02-18 22:08:06.224: ERROR/AndroidRuntime(355): Caused by: java.lang.NullPointerException
02-18 22:08:06.224: ERROR/AndroidRuntime(355): at com.test.web.Main.onCreate(Main.java:17)
(there where more logs just none of them pertained to the problem)
On a semi- related note how do you make code look like code on stackexchange?
So I'm guessing the webView isn't initializing correctly?
You're getting NullPointerException because in your main.xml file you have defined a TextView not a WebView.
Just change the TextView to WebView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<WebView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView" />
</LinearLayout>
I am trying to create a simple webview and I am getting FC on each start.
Here is my MainActivity.java file:
package com.jerdog.apps;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView myWebView = (WebView) findViewById(R.layout.main);
myWebView.loadUrl("http://www.google.com");
}
}
and here is my res/layout/main.xml file
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I am including my logcat from the moment I press Run in Eclipse at pastebin
http://pastebin.com/5trRw9Bd
This line:
WebView myWebView = (WebView) findViewById(R.layout.main);
is incorrect, and myWebView is null. Thus the following line throws the NullPointerException
Change the line to:
WebView myWebView = (WebView) findViewById(R.id.webview);
findViewById takes the id of the View you wish to find as an input, however you were passing in your layout. Changing this to the id of the WebView element from your main.xml file should fix the issue. There is a tutorial if you need a reference implementation.