2016-07-19 11 views
1

私のアプリケーションは、その中にwebViewを含むフラグメントをロードしますが、戻るボタンを押してください。 Webがnullであると言ってクラッシュします。どうしてか分かりません。助けてください。私はあなたのmWebViewWebViewFragment.java内で定義され、MainActivity.javamWebViewが異なるオブジェクトであり、値がnullであるWebViewFragmentヌルオブジェクトリファレンスで仮想メソッド 'boolean android.webkit.WebView.canGoBack()'を呼び出そうとしました

MainActivity

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.webkit.WebView; 

public class MainActivity extends FragmentActivity { 

    WebView mWebView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     WebViewFragment webViewFragment = new WebViewFragment(); 
     fragmentTransaction.replace(R.id.mainFragment, webViewFragment); 
     fragmentTransaction.commit(); 
    } 

    @Override 
    public void onBackPressed() { 

      if(mWebView.canGoBack()) { 
       mWebView.goBack(); 

      } 
    else 
     { 
      super.onBackPressed(); 
     } 
    } 

} 

WebViewFragment

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class WebViewFragment extends Fragment { 

    private WebView mWebView; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_main, container, false); 

     mWebView = (WebView) view.findViewById(R.id.webView); 

     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     mWebView.loadUrl("https://www.google.co.uk/"); 
     mWebView.setWebViewClient(new WebViewClient()); 

     return view; 
    } 

} 

答えて

0

でのようなアクセサを使用することができます。そのためmWebViewはヌルオブジェクトなので、mWebView.canGoBack()はエラーになります。あなたはMainActivity.javaでonBackPressed()を変更する必要があり

、例えば:

@Override 
public void onBackPressed() { 
    Fragment webview = getSupportFragmentManager().findFragmentByTag("webview"); 

    if ((webview instanceof WebViewFragment) && WebViewFragment.canGoBack()) { 
      WebViewFragment.goBack(); 
    } else { 
     super.onBackPressed(); 
    } 
} 

また、MainActivity.java、例えばにおけるラインfragmentTransaction.replace(...)の第三引数にユニークな識別フラグメントのタグを追加します"WebViewの":次に

fragmentTransaction.replace(R.id.mainFragment, webViewFragment, "webview"); 

mWebView静的class WebViewFragment内部ます

private static WebView mWebView; 

をそして、あなたを確保するには、class WebViewFragmentの内側に、この2つの静的メソッドを定義しました:

public static boolean canGoBack(){ 
    return myWebView.canGoBack(); 
} 

public static void goBack(){ 
    myWebView.goBack(); 
} 
関連する問題

 関連する問題