所以我正在使用带有加载进度条的webview来做一个应用程序,而我在几个活动中都这样做了,事情就是在一个活动中抛出这个
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.examplem16.vics_0.m16, PID: 30491
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
at com.examplem16.vics_0.m16.ServiciosActivity$MyWebViewClient.onPageStarted(ServiciosActivity.java:183)
at xl.b(SourceFile:219)
at agX.handleMessage(SourceFile:20)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
问题似乎是当我想告诉进度栏变为可见时,它无法识别它为已初始化
我检查了一种有效的活动并将其与该活动进行比较,但我似乎不知道出了什么问题
这是一项有效的活动
public class CursosActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_cursos);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setImageResource(R.drawable.ic_whatsapp_white_48dp);
fab.setBackgroundTintList(ColorStateList.valueOf(Color
.parseColor("#4674B7")));
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
onClickWhatsApp();
}
catch(Exception e)
{
}
};
});
spinner = (ProgressBar) findViewById(R.id.progressBar1); //initialize
spinner.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
String url = "https://mutual16.org/formacion-profesional/";
webView = (WebView) findViewById(R.id.webViewCursos);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new MyWebViewClient() {
@Override
public void onLoadResource(WebView view, String url) {
try {
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('header')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('footer')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByClassName('ssbp-container')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
webView.loadUrl("javascript:(function getStyle(elem, name) {" +
" // J/S Pro Techniques p136" +
" if (elem.style[name]) {" +
" return elem.style[name];" +
" } else if (elem.currentStyle) {" +
" return elem.currentStyle[name];" +
" }" +
" else if (document.defaultView && document.defaultView.getComputedStyle) {" +
" name = name.replace(/([A-Z])/g, \"-$1\");" +
" name = name.toLowerCase();" +
" s = document.defaultView.getComputedStyle(elem, \"\");" +
" return s && s.getPropertyValue(name);" +
" } else {" +
" return null;" +
" }" + "})()");
webView.loadUrl("javascript:(function() {" +
"var element = document.getElementById('page-container')," +
"padding = getStyle(element, 'paddingTop'); " +
"element.style.paddingTop = parseInt(padding, 0) + 0 + 'px';" + "})()");
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onPageFinished(WebView web, String url) {
super.onPageFinished(web, url);
spinner.setVisibility(View.INVISIBLE);
setTitle(web.getTitle());
}
}
);
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String urlHost = Uri.parse(url).getHost();
switch (urlHost) {
case "mutual16.org":
return false;
case "www.mutual16.org":
return false;
case "https://mutual16.org":
return false;
default:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
@Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
super.onPageStarted(webView, url, favicon);
spinner.setVisibility(View.VISIBLE); //same line with problem in the other activity
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
spinner.setVisibility(View.INVISIBLE);
setTitle(view.getTitle());
}
}
public void onClickWhatsApp() {
try {
PackageManager packageManager = this.getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
String url = "https://api.whatsapp.com/send?phone="+"543512002828" ;
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
this.startActivity(i);
}
} catch (Exception e){
e.printStackTrace();
}
}
}
,这里是它的xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".CursosActivity"
tools:layout_editor_absoluteY="81dp"
tools:showIn="@layout/activity_cursos">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<!-- #4674B7 -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/facebookm16blanco2"
android:layout_gravity="center"
android:id="@+id/toolbar_logo"
android:contentDescription="@string/content_description_logo_M16"/>
</android.support.v7.widget.Toolbar>
<WebView
android:id="@+id/webViewCursos"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
这是无效的活动
public class ServiciosActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_servicios);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setImageResource(R.drawable.ic_whatsapp_white_48dp);
fab.setBackgroundTintList(ColorStateList.valueOf(Color
.parseColor("#4674B7")));
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
onClickWhatsApp();
}
catch(Exception e)
{
}
};
});
spinner = (ProgressBar) findViewById(R.id.progressBar1); //initialize
String url = "https://mutual16.org/servicios/";
webView = (WebView) findViewById(R.id.webViewServicios);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new MyWebViewClient() {
@Override
public void onLoadResource(WebView view, String url)
{
try {
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('header')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('footer')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByClassName('ssbp-container')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
webView.loadUrl("javascript:(function getStyle(elem, name) {" +
" // J/S Pro Techniques p136" +
" if (elem.style[name]) {" +
" return elem.style[name];" +
" } else if (elem.currentStyle) {" +
" return elem.currentStyle[name];" +
" }" +
" else if (document.defaultView && document.defaultView.getComputedStyle) {" +
" name = name.replace(/([A-Z])/g, \"-$1\");" +
" name = name.toLowerCase();" +
" s = document.defaultView.getComputedStyle(elem, \"\");" +
" return s && s.getPropertyValue(name);" +
" } else {" +
" return null;" +
" }" + "})()");
webView.loadUrl("javascript:(function() {" +
"var element = document.getElementById('page-container')," +
"padding = getStyle(element, 'paddingTop'); " +
"element.style.paddingTop = parseInt(padding, 0) + 0 + 'px';" +"})()");
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
spinner.setVisibility(View.INVISIBLE);
setTitle(view.getTitle());
}
}
);
}
@Override
public void onBackPressed() {
if(webView.canGoBack())
{
webView.goBack();
}
else
{
super.onBackPressed();
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String urlHost = Uri.parse(url).getHost();
switch (urlHost) {
case "mutual16.org":
return false;
case "www.mutual16.org":
return false;
case "https://mutual16.org":
return false;
default:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
@Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
super.onPageStarted(webView, url, favicon);
spinner.setVisibility(View.VISIBLE); //line 183
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
spinner.setVisibility(View.INVISIBLE);
setTitle(view.getTitle());
}
}
public void onClickWhatsApp() {
try {
PackageManager packageManager = this.getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
String url = "https://api.whatsapp.com/send?phone="+"543512002828" ;
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
this.startActivity(i);
}
} catch (Exception e){
e.printStackTrace();
}
}
}
,这里是它的xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ServiciosActivity"
tools:showIn="@layout/activity_servicios">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize">
<!-- #4674B7 -->
<ImageView
android:id="@+id/toolbar_logo"
android:contentDescription="@string/content_description_logo_M16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/facebookm16blanco2" />
</android.support.v7.widget.Toolbar>
<WebView
android:id="@+id/webViewServicios"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
编辑:
按照@TheWanderer的要求添加activity_servicios和activity_cursos xmls
第一个有效的(activity_cursos)
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CursosActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_cursos" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
这里是无效的(activity_servicios)
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ServiciosActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_servicios" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
答案 0 :(得分:-1)
最后,我能够查明原因,我在那里有一个layout-v21文件夹和content_servicios xml,那里没有进度条,这是造成问题的原因,只是添加了进度条就可以了