I am using the Android SDK and PhoneGap to create a native Android App. Now I want to use Java methods in the view, calling by JS methods.
In the Main Class I called the "addJavascriptInterface" method to bind a java class with the view.
public class App extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
super.loadUrl("file:///android_asset/www/index.html");
}
}
The problem is I get an InvocationTarget Exception when the programm executes the line "appView.addJavascript..." and the program crashes on the device.
Any solutions here?
Thanks!
Calling Java methods from JavaScript is exactly what PhoneGap plugins enable. Checkout several examples here
Using addJavascriptInterface before super.init(); can make the application crash.
You should try the following :
public class App extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
appView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
super.loadUrl("file:///android_asset/www/index.html");
}
}
Related
I am very new to Cordova framework. I am trying to access HTML file from Droidgap activity, but getting exception as,
Cross-origin requests are only supported for protocol schemes: HTTP, data, chrome, chrome-extension, https.
For this I found one link, maybe you will find this question as a duplicate, but I am looking for Android solution, they are saying to install local server and then try to access this HTML, but in my case I want it to be accessible in MainActivity.
following is code..
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.appView.getSettings().setAllowFileAccess(true); //this line throws nullPointerException for appView
super.appView.getSettings().setAllowFileAccessFromFileURLs(true);
super.appView.getSettings().setAllowUniversalAccessFromFileURLs(true);
super.loadUrl("file:///android_asset/www/MyHtml.html");
}
}
My HTML is located at asset/www/MyHtml.html
I am really struggling with this issue, can anyone help to get out of this?
You say:
Cross-origin requests are only supported for protocol schemes: HTTP,
data, chrome, chrome-extension, https.
You also say (in comment):
In this case I am getting appView variable as null,
NullpointerException
First get the appView with the getView() method, and use a local variable:
WebView myappView = getView();//getView is a method from DroidGap super class
WebSettings settings = myappView.getSettings();
Your are not initializing DroidGap so add super.init(); as follows:
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();//you were missing this
WebView myappView = getView();//getView is a method from DroidGap super class
WebSettings settings = myappView.getSettings();
settings.setAllowFileAccess(true); //this line throws nullPointerException for appView
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
super.loadUrl("file:///android_asset/www/MyHtml.html");
}
}
You should be fine now but you can also try one (or all) of these (see WebSettings):
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
See DroidGap.java source code.
When I try to follow the guide of Cordova to embed it into a native android application I can't build.
I use this guide:
https://cordova.apache.org/docs/en/latest/guide/platforms/android/webview.html
When I put everything in my main activity I receive an error on config.
It says can't resolve symbol 'config'. I have no clue what I am doing wrong or how to solve it.
onCreate
/* Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cwv = (CordovaWebView) findViewById(R.id.tutorialView);
config.init(this);
cwv.loadUrl(config.getStartUrl());
}
I need to detect from a native JAVA (non phonegap plugin) class if phonegap's web view has finished loading. Is this possible, or will I need to rely on structuring my code as a plugin?
I finally found a solution (tested and works).
It's a tricky workaround but does the job.
Step 1:
Create a new class in the same package as your MainActivity like this:
public class MyClass{
private WebView appView;
public MyClass(DroidGap gap, WebView view){
appView = view;
}
public void onLoaded(){
// This will be called when webview loaded
System.out.println("WORKED!!");
}
}
Step 2:
Add the following lines to the very end of onCreate() in your MainActivity:
MyClass ai = new MyClass(this, super.appView);
super.appView.addJavascriptInterface(ai, "MyClass");
It should look something like this:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
super.loadUrl("file:///android_asset/www/index.html");
MyClass ai = new MyClass(this, super.appView);
super.appView.addJavascriptInterface(ai, "MyClass");
}
Step 3:
In your JavaScript code in assets/www add this:
document.addEventListener("deviceready", function(){
try{
// Might throw an error but onLoaded() gets called correctly
window.MyClass.onLoaded();
}
catch(e){
console.error(e)
}
});
Important: Make sure that cordova.js is correctly included in your index.html
This did the job for me.
I'm developing some sample code.
I had some problem using cocos2d.
That is the CCGLSurfaceView problem.
Firstly, I imported cocos2d.jar in my project with fps_images.png file.
And i modify my project for using the cocos2d library.
Here are the code.
public class ToadActivity extends Activity {
protected CCGLSurfaceView _glSurfaceView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_glSurfaceView = new CCGLSurfaceView(this);
setContentView(_glSurfaceView);
}
#Override
public void onStart()
{
super.onStart();
CCDirector.sharedDirector().attachInView(_glSurfaceView);
CCDirector.sharedDirector().setDisplayFPS(true);
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
}
}
==>>> _glSurfaceView = new CCGLSurfaceView(this);
Here, My application found a error. And at the result, it is killed in my android simulator.
What's the problem?
That library are decreated, not update more. It will be better start using: http://www.cocos2d-x.org/. It use c++, to work on android, use JNI (Native code). Code can be ported to other plataforms, like IOs.
Hope it help!
hello all i am very new to phonegap.. i want to webview into my application how can i add this? i've created same app. using android but how it can devleope using PHONEGAP?
private WebView mWebView;
//bla bla bla..
#Override
public void onCreate(Bundle savedInstanceState) {
mWebView = (WebView) findViewById(R.id.webviewHelp);
WebSettings webSettings = mWebView.getSettings();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyjavascriptInterface(), "HTMLOUT");
mWebView.loadUrl(strURL);
mWebView.setWebViewClient(new HelloWebViewClient());
}
public class MyjavascriptInterface {
public void showHTML(String html)
{
bla bla bla...
}
}
public class HelloWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
bla bla bla...
}
}
}
thanks in advance :Pragna
For your most basic PhoneGap app you should extend your main activity from DroidGap.
import com.phonegap.DroidGap;
public class Main extends DroidGap {
/** Called when the activity is first created. */
//private Button m_button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
I would suggest checking out this link for a series of tutorials using Phonegap within the Eclipse development environment. Once you are setup on eclipse, you only need to overide a few lines of code in the Android Activity class to call your web pages. Phonegap takes care of the rest in terms of opening up a webview class and rendering your html within it. You will no longer to code that yourself (as you've done above). The tutorials spell it out quite clearly. You can also add your own custom JavaScript interface methods as well. Again that's described in the tutorials. Hope this helps.
In your case there is a need to Embedding Cordova WebView on Android here is link for it
Modify your main Activity as
public class MainActivity 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");
}
And in your layout replace your webview with
<org.apache.cordova.CordovaWebView
android:id="#+id/tutorialView"
android:layout_width="match_parent"
android:layout_height="match_parent" />