-2

the app doesn't work I don't know what am I doing wrong please help me, there is code MainActivity.java:

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
final WebView mWebView = (WebView) findViewById(R.id.webView);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    String url = "https://www.webnotes.cz/";
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl(url);
    mWebView.setWebViewClient (new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            String user="u";
            String pwd="p";
            view.loadUrl("javascript:document.getElementById('UID0').value = '"+user+"';document.getElementById('PASS0').value='"+pwd+"';");
        }
    });
}

}

Thanks for your respond guys!

  • Possible duplicate of [Why Android app crashes for initializing variable with findViewById(R.id.\*\*\*\*\*\*) at the beginning of the class?](http://stackoverflow.com/questions/36446114/why-android-app-crashes-for-initializing-variable-with-findviewbyidr-id) – Mike M. Oct 29 '16 at 00:11
  • 1
    When you want help debugging a crash, you need to include the details of the crash, including when it happens, what actions cause it, and the stack trace generated in your logcat. – Mike M. Oct 29 '16 at 00:12
  • I guess `mWebView` is null and your are getting a NPE – Diego Torres Milano Oct 29 '16 at 01:56
  • Post the error log first – ADM Oct 29 '16 at 04:25

2 Answers2

1

Views should be initialized after inflating the view to activity, which is done using setContentView(). Move below line to onCreate() method after setContentView();

mWebView = (WebView) findViewById(R.id.webView);
Sanjeet
  • 2,385
  • 1
  • 13
  • 22
1

Note:- Initialize webview or any other view in onCreate method only... Because findviewById will not get id of that view before setting content view(layout)

public class MainActivity extends Activity {
    final WebView mWebView;
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.webView)
    }
}
Bhavnik
  • 2,020
  • 14
  • 21