2014-09-11 32 views
-1

我已經獲得了滑動菜單的源代碼並希望將webview應用於此,但似乎無法正常工作,並且無法找到明確的答案。這裏是我的代碼,並希望它會做出什麼,我想實現的意義:如何將Webview實現爲layoutinflator

import info.androidhive.slidingmenu.R; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.WebView; 

public class TestingPage extends Fragment { 

public TestingPage(){} //Have removed this code, but still gives same error 
private WebView webView; 

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

View rootView = inflater.inflate(R.layout.fragment_testpage, container); 

webView = (WebView) findViewById(R.id.Webview1); 
webView.getSettings().setBuiltInZoomControls(true); 
webView.getSettings().setLoadWithOverviewMode(true); 
webView.getSettings().setUseWideViewPort(true); 
webView.loadUrl("file:///android_asset/index.html"); 

return rootView; 

} 


} 

現在我的問題是findViewById下我與的選項的錯誤:「創建方法findViewById(INT)

當我創建這個(通過給出的選項),然後我得不到任何錯誤,然後這是我得到什麼?

private WebView findViewById(int webview1) { 
    // TODO Auto-generated method stub 
    return null; 
    } 
} 

現在我不知道該怎麼在這裏做,當我運行它,測試我得到這個應用程序:

09-11 13:39:07.649: E/AndroidRuntime(31488): FATAL EXCEPTION: main 
09-11 13:39:07.649: E/AndroidRuntime(31488): java.lang.NullPointerException 
09-11 13:39:07.649: E/AndroidRuntime(31488): at com.test.testapp.findViewById(TestingPage.java:33) 
09-11 13:39:07.649: E/AndroidRuntime(31488): at com.test.testapp.onCreateView(TestingPage.java:22) 
09-11 13:39:07.649: E/AndroidRuntime(31488): at android.app.Fragment.performCreateView(Fragment.java:1699) 
09-11 13:39:07.649: E/AndroidRuntime(31488): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903) 
09-11 13:39:07.649: E/AndroidRuntime(31488): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075) 
09-11 13:47:58.229: E/AndroidRuntime(32545): FATAL EXCEPTION: main 09-11 13:47:58.229: E/AndroidRuntime(32545): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first 

編輯

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<WebView 
    android:id="@+id/Webview1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

</RelativeLayout> 

使用正確的代碼EDITED

問題是這段代碼:

View rootView = inflater.inflate(R.layout.fragment_campsites, container); 

^h我解決了這個問題,最後加入了假:

View rootView = inflater.inflate(R.layout.fragment_campsites, container, false); 

現在一切正常工作。

回答

0

你是rootview內部的WebView有id爲根視圖

View rootView = inflater.inflate(R.layout.fragment_testpage, container); 
webView = (WebView) rootView .findViewById(R.id.Webview1); 
+0

非常感謝您照顧錯誤,但是現在運行應用程序時我收到了: – wemapps 2014-09-11 11:48:14

+0

09-11 13:47:58.229:E/AndroidRuntime(32545):致命例外:main 09-11 13:47:58.229:E/AndroidRuntime(32545):java.lang.IllegalStateException:指定的子項已具有父項。您必須先調用子對象的父對象的removeView()。 – wemapps 2014-09-11 11:49:12

+0

R.layout.fragment_testpage post xml – 2014-09-11 11:50:04

1

內使用此代碼

View rootView = inflater.inflate(R.layout.fragment_testpage, container); 

webView = (WebView) rootView.findViewById(R.id.Webview1); 
webView.getSettings().setBuiltInZoomControls(true); 
webView.getSettings().setLoadWithOverviewMode(true); 
webView.getSettings().setUseWideViewPort(true); 
webView.loadUrl("file:///android_asset/index.html"); 

取代你的代碼只是改變webView = (WebView) rootView.findViewById(R.id.Webview1);

0

變化
View rootView = inflater.inflate(R.layout.fragment_testpage, container);

View rootView = inflater.inflate(R.layout.fragment_testpage, container,false); 

缺乏充氣方法將導致加入「rootView」到「容器」,「假」的參數。 當在onCreateView方法中返回「rootView」時會引起異常

+0

我剛剛計算出並更新了我的問題 – wemapps 2014-09-11 12:50:08

+0

在你發佈你的答案之前 – wemapps 2014-09-11 12:50:24

+0

我會投你的答案,但我需要15分或更高,但考慮到這是一個投票! – wemapps 2014-09-11 12:51:21