-2

I am trying to make an app that has several features based off of features of a website I already have. Basically I am creating a companion app. That being said, the app crashes when I press a button on the main page to launch the feature. I have scoured the internet trying every single solution I could find but so far none have worked. The strange part is that it works as long as there is no additional code except for code generated upon creating the class.

Here is my Main Activity:

package com.example.nicole.signal;

import android.content.Intent;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import com.example.nicole.signal.R;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


// Button Calls

        Button forumbtn = (Button) findViewById(R.id.forum_btn);
        forumbtn.setOnClickListener(new View.OnClickListener()
        {
           @Override
            public void onClick (View view)
           {
                startActivity(new Intent(MainActivity.this, ForumActivity.class));
           }
        });

        Button webbtn = (Button) findViewById(R.id.Web_btn);
        webbtn.setOnClickListener(new View.OnClickListener()
        {
           @Override
            public void onClick (View view)
           {
               startActivity(new Intent(MainActivity.this, WebActivity.class));
           }
       });

       Button chatbtn = (Button) findViewById(R.id.Chat_btn);
        chatbtn.setOnClickListener(new View.OnClickListener()
       {
           @Override
           public void onClick (View view)
           {
              startActivity(new Intent(MainActivity.this, ChatActivity.class));
            }
        });

        Button videobtn = (Button) findViewById(R.id.Video_btn);
        videobtn.setOnClickListener(new View.OnClickListener()
        {
           @Override
           public void onClick (View view)
           {
                startActivity(new Intent(MainActivity.this, VideoActivity.class));
           }
        });

    }


    @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);
    }

}

and one of the classes I am calling that crash:

package com.example.nicole.signal;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class WebActivity extends MainActivity
{

    private final WebView mWebView = (WebView) findViewById(R.id.activity_web);


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Enable Javascript
      WebSettings webSettings = mWebView.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setJavaScriptEnabled(true);
        Uri uri = Uri.parse("http://hnsignal.ca");
      Intent intent = new Intent(Intent.ACTION_VIEW, uri);
       startActivity(intent);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
    }
}

here is the logcat error :

02-26 17:33:37.895 29086-29086/? E/Zygote: MountEmulatedStorage()
02-26 17:33:37.895 29086-29086/? E/Zygote: v2
02-26 17:33:37.915 29086-29086/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
02-26 17:33:48.976 29086-29086/com.example.nicole.signal E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.nicole.signal, PID: 29086
                                                                           java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.nicole.signal/com.example.nicole.signal.WebActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2546)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
                                                                               at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:145)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                               at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
                                                                               at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
                                                                               at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
                                                                               at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
                                                                               at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205)
                                                                               at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                                                                               at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                                                                               at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                                               at com.example.nicole.signal.WebActivity.<init>(WebActivity.java:14)
                                                                               at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                               at java.lang.Class.newInstance(Class.java:1650)
                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2536)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                                                                               at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:145) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5942) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

Any help would be greatly appreciated. During build no errors are thrown up so i imagine it is somewhere in my code... i just cant find it. Thank you

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Nikki Fraser
  • 3
  • 1
  • 1
  • 5
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Code-Apprentice Feb 26 '17 at 22:52
  • `at com.example.nicole.signal.WebActivity.(WebActivity.java:14)` The error occurs on line 14 of WebActivity.java. Which line is this? – Code-Apprentice Feb 26 '17 at 22:53
  • Views are created when you call `setContentView()`. You cannot access before you do this. – Code-Apprentice Feb 26 '17 at 22:54
  • `java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.webkit.WebView` This is a completely different error than your original question. Please post a new question. – Code-Apprentice Feb 26 '17 at 23:14
  • I should have suggested posting a new question as soon as you said you had made the suggested changes. I have rolled back your edit so that the below answers will make sense. Note that the stacktrace tells you the exact line that causes the problem. You should start there to help you figure out what is wrong. If you still need help, feel free to post a new question. Good luck! – Code-Apprentice Feb 26 '17 at 23:16
  • ok. thank you for the help so far. i will repost a new question if i need to. the original error resolved though. – Nikki Fraser Feb 26 '17 at 23:19

2 Answers2

2

There are several problems with your code. First of all

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);

These must be the first two lines of onCreate(). setContentView() creates the view objects, so you cannot access any of them until after you call this method. This also means that you cannot initialize field variables in-line as you do with:

    private final WebView mWebView = (WebView) findViewById(R.id.activity_web);

Instead, you must do this inside of onCreate(). You do this all correctly in MainActivity. Use that as a guide.

You should also change WebActivity to

public class WebActivity extends AppCompatActivity

You shouldn't extend MainActivity.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0
private final WebView mWebView = (WebView) findViewById(R.id.activity_web);

This code is the problem, you should declare the webview as

private final WebView mWebView;

and set its value after the setContentView like this

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebView = (WebView) findViewById(R.id.activity_web);
WebSettings webSettings = mWebView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptEnabled(true);
Uri uri = Uri.parse("http://hnsignal.ca");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

because setContentView will inflate the layout, and you cannot find a view using findViewById before the layout is inflated. and all your code for setting up the webview should be below the code for finding the webview

dito cesartista
  • 209
  • 1
  • 10