0

Iam爲「Drawerwith Swipe Tab」創建項目,因爲我在片段Class中使用了webview ,但在正在運行的應用越來越其崩潰得到與錯誤日誌貓,仍然得到錯誤嘗試在片段類中使用webview時調用空對象引用上的虛擬方法'void android.webkit.WebView.setWebViewClient()'類

嘗試上的空對象引用調用虛擬方法「無效android.webkit.WebView.setWebViewClient()」

片段級別:

對於片段級

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<WebView 
    android:id="@+id/web_book1" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentStart="false" 
    android:layout_alignParentEnd="true" /> 


    <ProgressBar 
    style="?android:attr/progressBarStyleLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/progressBar_book1" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

logcat的錯誤

package com.androidbelieve.drawerwithswipetabs; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; public class SocialFragment extends Fragment { ProgressBar pb_per; WebView mWebView; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { pb_per = (ProgressBar) container.findViewById(R.id.progressBar_book1); mWebView = (WebView) container.findViewById(R.id.web_book1); //This is the id you gave for webview //------------------------------------- to over ride keyboard error ------------(1) mWebView.setWebViewClient(new myWebClient()); mWebView.getSettings().setJavaScriptEnabled(true); //---------------------------------------------------------------------------- mWebView.getSettings().setSupportZoom(true); //Zoom Control on web (You don't need this //if ROM supports Multi-Touch mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM mWebView.setBackgroundColor(Color.parseColor("#FFFFFF")); mWebView.getSettings().setUseWideViewPort(true); mWebView.getSettings().setLoadWithOverviewMode(false); // Load URL mWebView.loadUrl("http://m.tnstc.in"); return inflater.inflate(R.layout.social_layout,null); } //================================= to over ride keyboard error (2)============================ public class myWebClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO Auto-generated method stub super.onPageStarted(view, url, favicon); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub pb_per.setVisibility(View.VISIBLE); // multi_per.setVisibility(ProgressBar.GONE); view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); pb_per.setVisibility(View.GONE); // multi_per.setVisibility(ProgressBar.VISIBLE); } } } 

XML代碼:

-10 18:03:52.724 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504636): avc: denied { search } for name="31316" dev="proc" ino=3076117 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:system_app:s0 tclass=dir permissive=0 
03-10 18:03:52.734 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504637): avc: denied { search } for name="32328" dev="proc" ino=2257596 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:bluetooth:s0 tclass=dir permissive=0 
03-10 18:03:52.734 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504638): avc: denied { search } for name="32328" dev="proc" ino=2257596 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:bluetooth:s0 tclass=dir permissive=0 
03-10 18:03:52.744 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504639): avc: denied { search } for name="32328" dev="proc" ino=2257596 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:bluetooth:s0 tclass=dir permissive=0 
03-10 18:03:52.764 3534-3534/? W/BackgroundThrea: type=1400 audit(0.0:5504640): avc: denied { search } for name="1" dev="proc" ino=6152 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=dir permissive=0 
03-10 18:03:52.900 31372-31372/? D/AndroidRuntime: Shutting down VM 
03-10 18:03:52.905 31372-31372/? E/AndroidRuntime: FATAL EXCEPTION: main 
                Process: com.androidbelieve.drawerwithswipetabs, PID: 31372 
                Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system} 
                java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference 
                 at com.androidbelieve.drawerwithswipetabs.SocialFragment.onCreateView(SocialFragment.java:30) 
                 at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) 
                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) 
                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) 
                 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) 
                 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) 
                 at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490) 
                 at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
                 at android.support.v4.view.ViewPager.populate(ViewPager.java:1105) 
                 at android.support.v4.view.ViewPager.populate(ViewPager.java:951) 
                 at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473) 
                 at android.view.View.measure(View.java:18797) 
                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                 at android.view.View.measure(View.java:18797) 
                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                 at android.view.View.measure(View.java:18797) 
                 at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868) 
                 at android.view.View.measure(View.java:18797) 
                 at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) 
                 at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) 
                 at android.view.View.measure(View.java:18797) 
                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                 at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124) 
                 at android.view.View.measure(View.java:18797) 
                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                 at android.view.View.measure(View.java:18797) 
                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                 at android.view.View.measure(View.java:18797) 
                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                 at android.view.View.measure(View.java:18797) 
                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                 at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2899) 
                 at android.view.View.measure(View.java:18797) 
                 at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 
                 at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 
                 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 
                 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 
                 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 
                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                at android.view.Choreographer.doCall 
03-10 18:03:52.976 791-2716/? W/ActivityManager: Force finishing activity com.androidbelieve.drawerwithswipetabs/.MainActivity 
03-10 18:03:52.982 791-1298/? W/SearchableInfo: Invalid searchable metadata for com.bt.bms/.activities.GlobalSearchActivity: Search label must be a resource reference. 
03-10 18:03:53.214 791-2716/? W/ActivityManager: Force finishing activity com.facebook.katana/.activity.ImmersiveActivity 
03-10 18:03:53.221 791-1276/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService.crashApplication(com.android.server.am.ProcessRecord, android.app.ApplicationErrorReport$CrashInfo) from ActivityManagerService.java:12872 waiters=0 for 305ms 
03-10 18:03:53.222 27217-27217/? D/AppStateLogger: Activity activity.ImmersiveActivity changed state to Stopped 
03-10 18:03:53.232 27217-27259/? D/AppStateLogger: Successfully dumped app state to log file 

回答

0

修改代碼的東西了這一點。佈局包含webview,ProgressBar而不是容器。

package testing.dev.hb.com.testing; 

import android.graphics.Bitmap; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ProgressBar; 

public class SocialFragment extends Fragment { 


    ProgressBar pb_per; 
    WebView mWebView; 
    View view; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     view = inflater.inflate(R.layout.social_layout, container, false); 

     pb_per = (ProgressBar) view.findViewById(R.id.progressBar_book1); 
     mWebView = (WebView) view.findViewById(R.id.web_book1); //This is the id you gave for webview 

     //------------------------------------- to over ride keyboard error ------------(1) 
     mWebView.setWebViewClient(new myWebClient()); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     //---------------------------------------------------------------------------- 


     mWebView.getSettings().setSupportZoom(true);  //Zoom Control on web (You don't need this 
     //if ROM supports Multi-Touch 
     mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM 
     mWebView.setBackgroundColor(Color.parseColor("#FFFFFF")); 
     mWebView.getSettings().setUseWideViewPort(true); 
     mWebView.getSettings().setLoadWithOverviewMode(false); 


     // Load URL 
     mWebView.loadUrl("http://m.tnstc.in"); 


     return view; 
    } 

//================================= to over ride keyboard error (2)============================ 

    public class myWebClient extends WebViewClient { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      // TODO Auto-generated method stub 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // TODO Auto-generated method stub 
      pb_per.setVisibility(View.VISIBLE); 
      // multi_per.setVisibility(ProgressBar.GONE); 

      view.loadUrl(url); 
      return true; 

     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 
      pb_per.setVisibility(View.GONE); 
      // multi_per.setVisibility(ProgressBar.VISIBLE); 

     } 
    } 
} 
+0

感謝您的幫助,我修改了代碼,但得到了新的錯誤「java.lang.NullPointerException:嘗試寫入字段'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager'on一個空對象引用「 – saurabh

+0

任何人都可以幫我出plz – saurabh

+0

在哪個類和哪裏? –

0

onCreateView方法你必須調用findViewByIdcontainer。 充氣一個新view,並從它初始化所有的觀點:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.social_layout, container); 
     pb_per = (ProgressBar) view.findViewById(R.id.progressBar_book1); 
     mWebView = (WebView) view.findViewById(R.id.web_book1); 
     mWebView.setWebViewClient(new WebViewClient() { 
       @Override 
       public boolean shouldOverrideUrlLoading(final WebView view, final String url) { 
        return false; 
       } 

       @Override 
       public void onPageStarted(final WebView view, final String url, final Bitmap favicon) { 
        pb_per.setVisibility(View.VISIBLE); 
        super.onPageStarted(view, url, favicon); 
       } 

       @Override 
       public void onPageFinished(final WebView view, final String url) { 
        pb_per.setVisibility(View.GONE); 
        super.onPageFinished(view, url); 
       } 
      }); 

     // mWebView settings... 

     // Load URL 
     mWebView.loadUrl("http://m.tnstc.in"); 

     return view; 
} 

而且你不需要實際myWebClient類了。你可以刪除它。

+0

仍然錯誤「java.lang.StackOverflowError:stack size 8MB」 – saurabh

+0

Process:com.androidbelieve.drawerwithswipetabs,PID:18360 Theme :主題:{default = overlay:system,iconPack:system,fontPkg:system,com.android.systemui = overlay:system,com.android.systemui.navbar = overlay:system} java.lang.StackOverflowError:stack size 8MB – saurabh

+0

嘗試它沒有drawerwithswipetabs。帶有WebView的片段應該可以工作。我想,現在你在其他地方有一個錯誤 – yital9

相關問題