-3

This is the basic code I have

package com.example.jorgesalgado.singular20;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;





public class MainActivity extends Activity {

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;

private LinearLayout dotsLayout;
private TextView[] dots;
private int[] layouts;
private Button btnNext;
private WebView mWebView;




@Nullable
@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,          WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




    mWebView = (WebView) findViewById(R.id.mWebView);
    mWebView.loadUrl("file:///android_asset/index.html");
    mWebView.getSettings().setJavaScriptEnabled(true);



    viewPager = (ViewPager) findViewById(R.id.view_pager);
    dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);

and this is the error

01-30 17:08:39.607 12741-12741/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process:     com.example.jorgesalgado.singular20, PID: 12741
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jorgesalgado.singular20/com.example.jorgesalgado.singular20.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
               ``                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:154)
                                                   at      android.app.ActivityThread.main(ActivityThread.java:6119)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
                                                   at com.example.jorgesalgado.singular20.MainActivity.onCreate(MainActivity.java:58)
                                                   at android.app.Activity.performCreate(Activity.java:6679)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:154) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

all im trying to do is to be able to use html and javascript in android studio using index.html . Do any of you have a better way to do this? if so could you please help me out.

here is the XML code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_screen1">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:id="@+id/linearLayout">

</LinearLayout>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:layout_above="@+id/textView2"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignStart="@+id/textView2"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:id="@+id/txtOutput" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Type in the topic"
    android:textColor="@android:color/white"
    android:textSize="@dimen/slide_title"
    android:textStyle="bold"
    android:id="@+id/textView2"
    android:layout_above="@+id/textView"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/desc_padding"
    android:paddingRight="@dimen/desc_padding"

    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textSize="@dimen/slide_desc"
    android:id="@+id/textView"
    android:layout_marginBottom="80dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="27dp"
    android:id="@+id/mWebView" />

  • It would seem that the `` with ID `mWebView` is not in the `activity_main` layout, either directly, or in an ``d layout. – Mike M. Jan 31 '17 at 01:25
  • @Electric Void post your XML – Charuක Jan 31 '17 at 03:36
  • add xml of your layout ? – W4R10CK Jan 31 '17 at 03:53
  • This is still not enough information to reopen. Which XML file is that exactly? Which folder is it in inside your project? Are you certain it's the one being loaded in your `MainActivity`? What shows on the screen if you comment out everything after the `setContentView()` call? Your Java code is looking not only for that `WebView`, but a `ViewPager`, and a `LinearLayout` with ID `layoutDots`. Where are those in your layout? Please read the linked duplicate to understand why you're getting that error, and refer to my first comment above. – Mike M. Feb 01 '17 at 05:45

1 Answers1

0

[Updated] Add tools:context=".MainActivity to your root layout

Matiullah Karimi
  • 1,234
  • 1
  • 15
  • 17