Reputation: 2979
creating dynamic html content in fragmenets and loading on embedded webview then it's say java.lang.NullPointerException at java.lang.String.indexOf(String.java:994)
public class DescFragment extends Fragment {
private CordovaWebView mCordovaWebview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View eventDescView = inflater.inflate(
R.layout.pf_fragment_event_desc_main, container, false);
eventDescView.setTag("Event Description");
mCordovaWebview = (CordovaWebView) eventDescView
.findViewById(R.id.event_detail_web_view);
mCordovaWebview.getSettings().setJavaScriptEnabled(true);
String html = "<html> <body> <p><font size=\"3\" font face=\"Arial\" style=\"font-weight:bold;\" color=\"black\">"
+ "</p>---------------------------------"
+ "</body></html>";
mCordovaWebview.loadData(html, "text/html", "UTF-8");
mCordovaWebview.setOnTouchListener(this);
}
return eventDescView;
}
public void onDestroy() {
super.onDestroy();
if (this.mCordovaWebview != null) {
this.mCordovaWebview.handleDestroy();
}
}
Logcat
FATAL EXCEPTION: main
java.lang.NullPointerException
at java.lang.String.indexOf(String.java:994)
at org.apache.cordova.CordovaWebView.loadUrlNow(CordovaWebView.java:471)
at org.apache.cordova.CordovaWebView.loadUrl(CordovaWebView.java:356)
at org.apache.cordova.CordovaWebViewClient.onPageFinished(CordovaWebViewClient.java:302)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:327)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
same code is working when am giving url but when am giving string formate in loadUrl app crashing thanks
Upvotes: 0
Views: 1693
Reputation: 2979
thnaks guys i got answer i have use below code then it woring fine
mCordovaWebview.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
Upvotes: 0
Reputation: 950
public void loadData (String data, String mimeType, String encoding)
Added in API level 1 Loads the given data into this WebView using a 'data' scheme URL.
Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. To avoid this restriction, use loadDataWithBaseURL() with an appropriate base URL.
The encoding parameter specifies whether the data is base64 or URL encoded. If the data is base64 encoded, the value of the encoding parameter must be 'base64'. For all other values of the parameter, including null, it is assumed that the data uses ASCII encoding for octets inside the range of safe URL characters and use the standard %xx hex encoding of URLs for octets outside that range. For example, '#', '%', '\', '?' should be replaced by %23, %25, %27, %3f respectively.
The 'data' scheme URL formed by this method uses the default US-ASCII charset. If you need need to set a different charset, you should form a 'data' scheme URL which explicitly specifies a charset parameter in the mediatype portion of the URL and call loadUrl(String) instead. Note that the charset obtained from the mediatype portion of a data URL always overrides that specified in the HTML or XML document itself.
Parameters
data a String of data in the given encoding
mimeType the MIME type of the data, e.g. 'text/html'
encoding the encoding of the data
This is a java doc for loadData(). It says that data is formatted URL to your page.
Here is an example from Phonegap.com
public class CordovaViewTestActivity extends Activity implements CordovaInterface {
CordovaWebView cwv;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cwv = (CordovaWebView) findViewById(R.id.tutorialView);
cwv.loadUrl("file:///android_asset/www/index.html");
}
Upvotes: 2