设置进度条类型微调器的可见性时,应用程序崩溃
When setting visibility of progress bar type spinner the app crashes
我正在构建一个打开 webview 的应用程序,当它打开网页时,我想显示一个微调器类型的进度条,但是当我使用 setvisibility()
更改可见性时,应用程序崩溃了。
这是我的 Activity 代码:
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);
}
}
登录猫
3:08:57 PM Gradle 构建在 9 秒内完成
3:22:44 PM Gradle 构建在 35 秒内完成
3:24:41 PM Gradle 构建在 7 秒内完成
3:24:44 私信 Session 'app': 运行
3:26:47 PM Gradle 构建在 6 秒内完成
3:26:49 私信 Session 'app': 运行
3:30:31 PM Gradle 构建在 13 秒内完成
3:30:34 私信 Session 'app': 运行
3:38:54 PM Gradle 构建在 9 秒内完成
3:39:02 私信 Session 'app': 运行
3:39:02 私信 Session 'app': 运行
3:39:04 PM Gradle 构建在 10 秒内完成
3:39:06 PM Session 'app': 调试器已连接
3:57:21 PM Gradle 构建在 11 秒内完成
3:57:24 私信 Session 'app': 运行
11:16:08 上午 Gradle 构建在 37 秒内完成
11:16:11上午Session'app':运行
11:52:39 AM Gradle 构建在 11 秒内完成,但出现 2 个错误
11:55:00 上午 Gradle 构建在 28 秒内完成
11:58:45 上午 Gradle 构建在 18 秒内完成
11:59:04 上午 Gradle 构建在 6 秒内完成
11:59:09上午Session'app':运行
3:51:43 PM Gradle 构建在 25 秒内完成
3:53:31 PM Gradle 构建在 40 秒内完成
3:53:33 私信 Session 'app': 运行
3:56:04 PM Gradle 构建在 2 秒内完成
3:56:07 私信 Session 'app': 运行
3:56:51 PM Gradle 构建在 2 秒内完成
4:04:03 PM Gradle 构建在 6 秒内完成
4:04:07 私信 Session 'app': 运行
4:04:54 PM Gradle 构建在 4 秒内完成
4:04:56 私信 Session 'app': 运行
4:05:34 PM Gradle 构建在 3 秒内完成
4:05:37 私信 Session 'app': 运行
4:08:51 PM Gradle 构建在 32 秒内完成
4:09:00 私信 Session 'app': 运行
4:13:42 PM Gradle 构建在 36 秒内完成
4:13:44 私信 Session 'app': 运行
4:50:31 PM Gradle 构建在 14 秒内完成
4:50:36 私信 Session 'app': 运行
5:04:19 PM Gradle 构建在 10 秒内完成
5:04:22 私信 Session 'app': 运行
您应该致电:
setContentView(R.layout.activity_main);
调用前:
pro = (ProgressBar) findViewById(R.id.progress);
因为在寻找子视图之前必须先设置视图
我正在构建一个打开 webview 的应用程序,当它打开网页时,我想显示一个微调器类型的进度条,但是当我使用 setvisibility()
更改可见性时,应用程序崩溃了。
这是我的 Activity 代码:
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);
}
}
登录猫
3:08:57 PM Gradle 构建在 9 秒内完成 3:22:44 PM Gradle 构建在 35 秒内完成 3:24:41 PM Gradle 构建在 7 秒内完成 3:24:44 私信 Session 'app': 运行 3:26:47 PM Gradle 构建在 6 秒内完成 3:26:49 私信 Session 'app': 运行 3:30:31 PM Gradle 构建在 13 秒内完成 3:30:34 私信 Session 'app': 运行 3:38:54 PM Gradle 构建在 9 秒内完成 3:39:02 私信 Session 'app': 运行 3:39:02 私信 Session 'app': 运行 3:39:04 PM Gradle 构建在 10 秒内完成 3:39:06 PM Session 'app': 调试器已连接 3:57:21 PM Gradle 构建在 11 秒内完成 3:57:24 私信 Session 'app': 运行 11:16:08 上午 Gradle 构建在 37 秒内完成 11:16:11上午Session'app':运行 11:52:39 AM Gradle 构建在 11 秒内完成,但出现 2 个错误 11:55:00 上午 Gradle 构建在 28 秒内完成 11:58:45 上午 Gradle 构建在 18 秒内完成 11:59:04 上午 Gradle 构建在 6 秒内完成 11:59:09上午Session'app':运行 3:51:43 PM Gradle 构建在 25 秒内完成 3:53:31 PM Gradle 构建在 40 秒内完成 3:53:33 私信 Session 'app': 运行 3:56:04 PM Gradle 构建在 2 秒内完成 3:56:07 私信 Session 'app': 运行 3:56:51 PM Gradle 构建在 2 秒内完成 4:04:03 PM Gradle 构建在 6 秒内完成 4:04:07 私信 Session 'app': 运行 4:04:54 PM Gradle 构建在 4 秒内完成 4:04:56 私信 Session 'app': 运行 4:05:34 PM Gradle 构建在 3 秒内完成 4:05:37 私信 Session 'app': 运行 4:08:51 PM Gradle 构建在 32 秒内完成 4:09:00 私信 Session 'app': 运行 4:13:42 PM Gradle 构建在 36 秒内完成 4:13:44 私信 Session 'app': 运行 4:50:31 PM Gradle 构建在 14 秒内完成 4:50:36 私信 Session 'app': 运行 5:04:19 PM Gradle 构建在 10 秒内完成 5:04:22 私信 Session 'app': 运行
您应该致电:
setContentView(R.layout.activity_main);
调用前:
pro = (ProgressBar) findViewById(R.id.progress);
因为在寻找子视图之前必须先设置视图