0

I'm trying to make my own browser, but I'm stuck. I try to send URL with string as data type using intent from my main activity to the web activity. This is my web activity java code:

public class webView extends MainActivity {

private WebView halamanWeb;
private ProgressBar progressBar;
private EditText searchBox;
private ImageButton tombolCari2;
private String Url;

private String getUrl(){
    return Url = "https://" + searchBox.getText().toString();
}

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

    searchBox = (EditText) findViewById(R.id.searchBoxAtas);
    tombolCari2 = (ImageButton) findViewById(R.id.tombolCari);
    progressBar = (ProgressBar) findViewById(R.id.bar);
    halamanWeb = (WebView) findViewById(R.id.webLayout);

    Intent test = getIntent();
    final String url = "https://" + test.getStringExtra("key");

    halamanWeb.getSettings().setJavaScriptEnabled(true);
    halamanWeb.getSettings().setDisplayZoomControls(true);
    halamanWeb.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(newProgress);
             if (newProgress == 100) {
               progressBar.setVisibility(View.GONE);
            }
            halamanWeb.loadUrl(url);
            halamanWeb.setWebViewClient(new MyWebLaunch());
        }

    });

    tombolCari2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            halamanWeb.getSettings().setJavaScriptEnabled(true); 
            halamanWeb.getSettings().setDisplayZoomControls(true);
            halamanWeb.setWebChromeClient(new WebChromeClient(){
                @Override
                public void onProgressChanged(WebView view, int newProgress){
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(newProgress);
                    if(newProgress == 100){
                        progressBar.setVisibility(View.GONE);
                    }
                }

            });
            halamanWeb.loadUrl(getUrl());
            halamanWeb.setWebViewClient(new MyWebLaunch());
        }
    });

}
private class MyWebLaunch extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return super.shouldOverrideUrlLoading(view, url);
    }
}
}

this is my main activity java code :

public class MainActivity extends AppCompatActivity {

//deklarasi
private EditText searchBox;
private ImageButton tombolCari;
private WebView halamanWeb;
private ProgressBar progressBar;
private String Url;

//deklarasi fungsi
private String getUrl(){
    return Url = "https://" + searchBox.getText().toString();
}

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

    searchBox = (EditText) findViewById(R.id.searchBoxAtas);
    tombolCari = (ImageButton) findViewById(R.id.tombolCari);
    progressBar = (ProgressBar) findViewById(R.id.bar);

    tombolCari.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Url = getUrl();
            Intent test = new Intent(v.getContext(), webView.class);
            test.putExtra("key", Url);
            startActivity(test);
        }
    });

}
}

when I run that code, got error code that say:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Can anyone help me with the intent idea, I want to pass a string URL from my main activity to the web activity and then use that to open the site using the URL. How can I get out from the null pointer on image button? I would like some help! Thanks!

Edit : This is my log when i start the code, it runs but when i input something and i pressed the ImageButton it crashed.

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.example.johno.myapplication.webView.onCreate(webView.java:54)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
John Owen
  • 3
  • 3

3 Answers3

1

Check if you have something like below in your surfing_layout.xml:

    <ImageButton
        android:id="@+id/tombolCari"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

If it is there, just check if the ID of that image button is tombolCari. If it is something else, just change it to android:id="@+id/tombolCari"

  • Thanks for your answer, i already checked my xml and i already declare that imagebutton and the id is assigned to "@+id/tombolCari" just the same as what you type. And its still doesnt work – John Owen Oct 15 '18 at 00:44
  • Oops, I'm sorry. Yeah after i checked it twice, i found that my xml files does contain ImageButton, but the id isn't "@+id/tombolCari". And yes it does solve one of my problem. But can you help me so i could pass some string Url variable from EditText in the MainActivity.java and use it to open the website using the string url given from the EditText in MainActivity.java (activity_main.xml)? – John Owen Oct 15 '18 at 10:06
  • What i meant is i want to pass some string Url variable from EditText in the MainActivity.java and use it to open the website using the string url given from the EditText in MainActivity.java (activity_main.xml) in the webView.java (surfing_activity.xml)? – John Owen Oct 15 '18 at 14:21
  • webView.java is a different activity? in that case, you dont want to extend MainActivity.java. Just extend AppCompatActivity. Like this - "public class webView extends AppCompatActivity" – Karthic Srinivasan Oct 15 '18 at 14:38
  • Thanks for answering me. Yes it is a different activity. So in this case i don't need to extend webView.java from MainActivity.java? And just extend it to AppCompatActivity? But why? Can you explain more to me, please? I want to learn a lot more things that i dont know. Thanks – John Owen Oct 15 '18 at 23:16
  • No you dont want to extend mainactivity in your webview. Check this link for more detailed explanations : https://stackoverflow.com/questions/32984955/how-to-open-web-page-within-my-app – Karthic Srinivasan Oct 16 '18 at 15:11
1

You are overriding MainActivity view with WebView activity and you are expecting MainActivity view should be alive.All your Mainactivity UI elements which are present in R.layout.activity_main will be out of scope.To fix this move to activity_main content to surfing_layout.

  Remove setContentView(R.layout.activity_main) and  from MainActivity and make surev 
   surfing_layout has tombolCari imageButton.
Ramesh Yankati
  • 1,197
  • 9
  • 13
  • Thanks for answering. Sorry but, Can you explain more to me about what happened? I cant really understand what you mean – John Owen Oct 15 '18 at 00:50
0

Now you have two xml one for MainActivity => "activity_main", and the second for webView => "surfing_layout".

I see that you extended MainActivity in webView Class and added the same widget and write it again in webView Class, then if you forget to add ImageButton from activity_main.xml to surfing_layout.xml, or forget to add the same id for ImageButton to surfing_layout.xml

Add this code to surfing_layout.xml

<ImageButton
    android:id="@+id/tombolCari"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  • Thanks! Thats helping me so much. Now i can run my code, but it doesn't run like what i wanted. I want to pass some string Url variable from EditText in MainActivity.java (activity.xml) and use that string to open website in the webview in the webView.java(surfing_layout.xml). Can you help me on that thing? – John Owen Oct 15 '18 at 10:09