0
public class MainActivity extends AppCompatActivity {
WebView webview;
//I did this 
Intent intent = getIntent();
    String I = intent.getStringExtra( "Author" );
// and removed this 
String url="http://pixelay.com/";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


webview= (WebView) findViewById(R.id.webview);
new MyAsynTask().execute();
}

private class MyAsynTask extends AsyncTask<Void, Void, Document> {
@Override
protected Document doInBackground(Void... voids) {

    Document document = null;
    try {
                         // Here
        document= Jsoup.connect(I).get();
        document.getElementsByClass("main-navigation").remove();
        document.getElementsByClass("custom-header-image").remove();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return document;
}

@Override
protected void onPostExecute(Document document) {
    super.onPostExecute(document);
                      //Here
webview.loadDataWithBaseURL(I,document.toString(),"text/html","utf-8","");
    webview.getSettings().setCacheMode( 
WebSettings.LOAD_CACHE_ELSE_NETWORK );

    webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, 
WebResourceRequest request) {
                  //Here 
            view.loadUrl(I);
            return super.shouldOverrideUrlLoading(view, request);
        }
    });
 }
}

But it does not work

Logcat shows this

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object

How can I can do that? Please help me

  • Its just a Url string no matter from where you get it .. the jsoup part will be same .. Please edit your question with the exact problem you are facing . is in parsing or in intent its not clear right now . – ADM Aug 19 '22 at 04:43
  • Move all `getIntent()` code inside `onCreate()` just before `new MyAsynTask().execute()` and make sure you are passing `Author` key in the Intent from Previous `Activity` .. Also you shouldn't be using `AsyncTask` anymore its [deprecated](https://stackoverflow.com/questions/58767733/the-asynctask-api-is-deprecated-in-android-11-what-are-the-alternatives) long ago . – ADM Aug 19 '22 at 05:30

0 Answers0