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)