Reputation: 61
I am building an app that open a webview and while it is opening the webpage i want to show a spinner type progressbar but when i change the visibility using setvisibility()
the app crashes .
Here is my Activity code:
public class MainActivity extends Activity {
WebView website;
ProgressBar pro;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pro= (ProgressBar) findViewById(R.id.progress);
setContentView(R.layout.activity_main);
website = (WebView) findViewById(R.id.website);
website.setWebViewClient(new WebViewClient());
WebSettings webSettings = website.getSettings();
webSettings.setJavaScriptEnabled(true);
website.loadUrl("http://www.google.com");
website.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
//change your progress bar
if(pro.getVisibility()==view.VISIBLE){
pro.setVisibility(view.INVISIBLE);
}
else {
pro.setVisibility(view.VISIBLE);
}
}
});
}
@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);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode ==KeyEvent.KEYCODE_BACK) && website.canGoBack()) {
website.goBack();
return true;
}
else{
finish();
}
return super.onKeyDown(keyCode, event);
}
}
Log cat
3:08:57 PM Gradle build finished in 9 sec 3:22:44 PM Gradle build finished in 35 sec 3:24:41 PM Gradle build finished in 7 sec 3:24:44 PM Session 'app': running 3:26:47 PM Gradle build finished in 6 sec 3:26:49 PM Session 'app': running 3:30:31 PM Gradle build finished in 13 sec 3:30:34 PM Session 'app': running 3:38:54 PM Gradle build finished in 9 sec 3:39:02 PM Session 'app': running 3:39:02 PM Session 'app': running 3:39:04 PM Gradle build finished in 10 sec 3:39:06 PM Session 'app': debugger connected 3:57:21 PM Gradle build finished in 11 sec 3:57:24 PM Session 'app': running 11:16:08 AM Gradle build finished in 37 sec 11:16:11 AM Session 'app': running 11:52:39 AM Gradle build finished with 2 error(s) in 11 sec 11:55:00 AM Gradle build finished in 28 sec 11:58:45 AM Gradle build finished in 18 sec 11:59:04 AM Gradle build finished in 6 sec 11:59:09 AM Session 'app': running 3:51:43 PM Gradle build finished in 25 sec 3:53:31 PM Gradle build finished in 40 sec 3:53:33 PM Session 'app': running 3:56:04 PM Gradle build finished in 2 sec 3:56:07 PM Session 'app': running 3:56:51 PM Gradle build finished in 2 sec 4:04:03 PM Gradle build finished in 6 sec 4:04:07 PM Session 'app': running 4:04:54 PM Gradle build finished in 4 sec 4:04:56 PM Session 'app': running 4:05:34 PM Gradle build finished in 3 sec 4:05:37 PM Session 'app': running 4:08:51 PM Gradle build finished in 32 sec 4:09:00 PM Session 'app': running 4:13:42 PM Gradle build finished in 36 sec 4:13:44 PM Session 'app': running 4:50:31 PM Gradle build finished in 14 sec 4:50:36 PM Session 'app': running 5:04:19 PM Gradle build finished in 10 sec 5:04:22 PM Session 'app': running
Upvotes: 0
Views: 615
Reputation: 12347
You should call:
setContentView(R.layout.activity_main);
before calling:
pro = (ProgressBar) findViewById(R.id.progress);
Because you have to set a view before looking for a child subview.
Upvotes: 1