2016-12-19 15 views
0

私はドロワーメニューを持っています。私のメニューページはそれぞれフラグメントです。そのうちの一つで、私はWebViewのを開くしようとしていますが、私はこの取得:フラグメントでwebviewを使用すると、アンドロイドで失敗します

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference 
     at com.example.niloofar.showroom.AboutFragment.onCreateView(AboutFragment.java:29) 
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5221) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

これは私のフラグメントのコードです:

package com.example.niloofar.showroom; 

import android.app.Activity; 
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; 
import android.widget.Button; 
import android.widget.EditText; 


public class AboutFragment extends Fragment { 


    private WebView wv1; 
    @Override 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     View v=inflater.inflate(R.layout.fragment_about, container, false); 
     wv1=(WebView)getActivity().findViewById(R.id.webView); 
     wv1.setWebViewClient(new MyBrowser()); 

       String url = "http://bding.ir"; 
     WebSettings webSettings = wv1.getSettings(); 

     wv1.getSettings().setLoadsImagesAutomatically(true); 
       wv1.getSettings().setJavaScriptEnabled(true); 
       wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
       wv1.loadUrl(url); 
     return v; 

    } 

    private class MyBrowser extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    } 
} 

を、これはそのレイアウトです:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.niloofar.showroom.AboutFragment"> 

<WebView 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:id="@+id/webView" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentBottom="true" /> 

これは私がフラグメントと呼ぶメニューです:

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
    Fragment fragment=null; 

    if (id == R.id.nav_home) { 
     // Handle the camera action 
     fragment = new ProfileFragment(); 
    } else if (id == R.id.nav_points) { 
     fragment = new PointFragment(); 
    } else if (id == R.id.nav_coupons) { 
     fragment = new CouponFragment(); 
    } else if (id == R.id.nav_about) { 
     fragment = new AboutFragment(); 
    } 
    fragmentTransaction.replace(R.id.appbar, fragment,fragment.getClass().getSimpleName()); 
    fragmentTransaction.commit(); 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

答えて

0

私はこれを行っている必要があります。

View v=inflater.inflate(R.layout.fragment_about, container, false); 
    wv1=(WebView)v.findViewById(R.id.webView); 
0

のWebViewがフラグメントである場合、あなたは以下のようなフラグメントビューからそれを取得する必要があります。

View v = inflater.inflate(R.layout.fragment_about、container、false); wv1 =(WebView)v.findViewById(R.id.webView);

0

View v=inflater.inflate(R.layout.fragment_about, container, false);  wv1=(WebView)v.findViewById(R.id.webView); 
へ変化以下のコード

View v=inflater.inflate(R.layout.fragment_about, container, false); 
    wv1=(WebView)getActivity().findViewById(R.id.webView);