2017-11-07 3 views
0

これは初めてです。 WebViewでウェブページを表示したいだけですが、この特定のアクティビティを起動している間に、それは が連続してクラッシュします。助けてください。 は、ここに私のコードWebViewクラッシュランタイム

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.webkit.WebView; 
import android.widget.Button; 

public class vistaweb extends AppCompatActivity implements View.OnClickListener { 

    String url= "https://myschoolserver.com/apps_login.php"; 


    WebView vWeb = (WebView) findViewById(R.id.MyWeb); 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getSupportActionBar().hide(); 
     setContentView(R.layout.activity_vistaweb); 




     vWeb.clearCache(true); 
     vWeb.clearHistory(); 
     vWeb.getSettings().setJavaScriptEnabled(true); 
     // vWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
     vWeb.loadUrl(url); 

     Button back = (Button)findViewById(R.id.back); 
     Button forward = (Button)findViewById(R.id.forward); 
     Button reload = (Button)findViewById(R.id.relode); 

     back.setOnClickListener(this); 
     forward.setOnClickListener(this); 
     reload.setOnClickListener(this); 


    } 
    @Override 
    public void onClick(View view) { 
     switch (view.getId()){ 
      case R.id.back: 
       vWeb.canGoBack(); 

       break; 

      case R.id.forward: 
       vWeb.canGoForward(); 
       break; 

      case R.id.relode: 
       vWeb.reload(); 
       break; 
     } 
    } 
} 

であり、ここでは私のxmlファイル

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    tools:context="com.myschoolserver.myschoolserver_beta.vistaweb"> 

    <Button 
     android:id="@+id/back" 
     style="@style/Widget.AppCompat.Button.Small" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="3dp" 
     android:text="BACK" 
     app:layout_constraintLeft_toLeftOf="parent" 
     tools:layout_editor_absoluteY="461dp" 
     android:layout_marginStart="3dp" /> 

    <Button 
     android:id="@+id/relode" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RELOAD" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_editor_absoluteY="462dp" /> 

    <Button 
     android:id="@+id/forward" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Forward" 
     tools:layout_editor_absoluteY="462dp" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" /> 

    <WebView 
     android:id="@+id/MyWeb" 
     android:layout_width="0dp" 
     android:layout_height="445dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginTop="16dp" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 
</android.support.constraint.ConstraintLayout> 

そして、起動中に、私は のWebView vWeb =(WebViewの)findViewById(Rである16行目でエラーを示すレポートのバグに気づきました.id.MyWeb);

助けてください。前もって感謝します。

答えて

0

1. onCreateの方法でfindViewByIdに電話する必要があります。

onClickの方法でvWebを使用していますので、vWebをグローバル変数に設定する必要があります。

3.追加許可を忘れないでください。あなたのコード内

<uses-permission android:name="android.permission.INTERNET"/> 

4.Set webView.setWebViewClientwebview.setWebChromeClient

private WebView vWeb; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getSupportActionBar().hide(); 
    setContentView(R.layout.activity_vistaweb); 
    vWeb = (WebView) findViewById(R.id.MyWeb); 
    ... 
} 
+0

はい、私は自分が望む正確な方法を理解するのに役立ちます。ありがとうございます。心から感謝する。 –