0

I am using a WebView in the android activity to load a web page when the activity created . The main reason is to execute javascript code from android . I do not have a good knowledge in android programming so after some googling I found this question :How to execute JavaScript on Android?

I created a project and tried the solution but error is occuring when initializing the activity. This is the code in the activity :

  package com.example.test;

  import android.net.Uri;
  import android.os.Bundle;
  import android.app.Activity;
  import android.content.Intent;
  import android.util.Log;
  import android.view.Menu;
  import android.webkit.WebView;

  public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView myWebView = (WebView) findViewById(R.id.webview);
   //       myWebView.getSettings().setJavaScriptEnabled(true); 
    myWebView.loadUrl("http://www.google.com");


}

public Activity getActivity() {
    Log.i("TAG", "getting activity");
    return this;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

This the code in the AndroidManifest.xml:

   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.test"
     android:versionCode="1"
   android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

   <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.test.MainActivity"
        android:label="@string/app_name" >
           <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/webview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
    android:name="com.example.DialogActivity"
    android:label="@string/app_name" 
    android:theme="@android:style/Theme.Dialog">
    </activity>
    <service android:enabled="true" android:name=".BgService" />

    </application>


 </manifest>

This is the error :

    FATAL EXCEPTION: main

    java.lang.RuntimeException: Unable to start activity     
    ComponentInfo{com.example.test/com.example.test.MainActivity}: 
   java.lang.NullPointerException

Any Help please bout the error?

1
  • dont define the webview in the manifest but in the activity layout .And set the layout in the activity with setContentView() Commented Jun 28, 2014 at 11:24

1 Answer 1

1
You not set your View 

setContentView(R.layout.activity_main);

in web view You can use method

in oncreate You put that code

 WebView web = (WebView) findViewById(R.id.webView);
web.getSettings().setJavaScriptEnabled(true);

if work than vote up.. Thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.