2017-11-07 1 views
0

Ich bin neu auf diesem. Ich möchte nur eine Webseite über WebView anzeigen, aber sie stürzt während des Starts dieser bestimmten Aktivität kontinuierlich ab. Bitte helfen Sie. Hier ist mein CodeWebView abstürzt Laufzeit

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; 
     } 
    } 
} 

Und hier ist meine XML-Datei

<?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> 

Und während ich den Bericht Fehler bemerkt Start in Zeile zeigt Fehler 16, die WebView vWeb = (WebView) findViewById (R .id.MyWeb);

Bitte helfen. Danke im Voraus.

Antwort

0

1.Sie sollten findViewById in onCreate Methode anrufen.

2. Weil Sie vWeb in onClick Methode verwenden. So sollten Sie vWeb als globale Variable festlegen.

3.Vergessen Sie nicht, die Erlaubnis hinzuzufügen.

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

4.Set webView.setWebViewClient und webview.setWebChromeClient in Ihrem Code.

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

Ja helfen Sie mir, genau zu verstehen, wie ich will. Danke mein Herr. Schätze es wirklich. –