Unable to resolve error in ctx.getContext() in phonegap 3.4 - android

Here I am tring to call an activity from a phonegap 3.4 plugin and this is the error being shown in the following code.
public void startActivity() {
Intent myIntent = new Intent(ctx.getContext(), SecActivity.class);
ctx.startActivity(myIntent);
}
"ctx" is showing an error.
But the same code worked with phonegap 1.6.
With what should I replace ctx?

ctx is a legacy member and is deprecated since the release of phonegap 2.0.
Check this link http://docs.phonegap.com/en/2.7.0/guide_plugin-development_android_index.md.html
To rectify the error try replacing ctx.getContext() with this.cordova.getActivity()

Related

AndroidHttpClient not found (when running Robolectric)

I've set up a very simple project to test the integration of Robolectric + Data Binding + Retrolambda. When I run the test suit, I get the following message:
Error:(30, 30) Gradle: error: cannot access AndroidHttpClient
class file for android.net.http.AndroidHttpClient not found
This is pretty odd since I don't use AndroidHttpClient anywhere.
The error occurs here, on the "activity" line:
#Before
public void setup() {
activity = Robolectric.setupActivity(MainActivity.class); // Error on this line
textView = (TextView) shadowOf(activity).findViewById(R.id.textView);
button = (Button) activity.findViewById(R.id.button);
editText = (EditText) activity.findViewById(R.id.editText);
}
The program never uses AndroidHttpClient. In fact, this is the entire program:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setUser(new User());
binding.button.setOnClickListener((v) -> {
binding.textView.setText(String.format("Hello, %s!", binding.editText.getText()));
binding.editText.setText("");
});
}
Ideas as to what's wrong?
AndroidHttpClient was removed from the SDK in v23 of the build tools.
As Robolectric is running against earlier versions, it expects it to be there, which is why you're seeing this error.
For now, you can add it back in:
android {
useLibrary 'org.apache.http.legacy'
}
As detailed here.
There is a GitHub ticket open for Robolectric to fix this. You can follow the thread/ticket here.
Update:
As some people have correctly pointed out, a better way of doing this would be to create a class android.net.http.AndroidHttpClient in your test resources. This would be a preferred method because you're only modifying the test sources, not the production code, in order to accommodate the tests.
I've just added fake class android.net.http.AndroidHttpClient in my test sources. And it solved the issue for now. Waiting for Robolectric to be updated
Apparent problem and solution:
AndroidHttpClient was removed from the SDK in API Level 23, while Robolectric was set to run tests with SDK 21:
AndroidHttpClient was removed from the SDK in API Level 23
I was able to solve this problem by creating a new class called AndroidHttpClient within a new package android.net.http. After that I had to annotate my Unit Test class with #Config(constants = BuildConfig.class, sdks = 21) which will run the tests against an emulated version of API 21 which is the last version of Android Robolectric supports currently.
There is currently an issue opened here, so once they release version 3.1 everything should be fine and you won't have to use this workaround.
If target SDK is 28 or greater then according to this, we have to put following line in AndroidManifest.xml
<uses-library android:name="org.apache.http.legacy" android:required="false"/>

tess-two can't find libpng.so

I have followed building instructions for tess-two on
Github
I build tess-two using NDK successfully and imported the library
I am trying to run the test application provided on the same repository but whenever the application starts it gives the following exception:
That error is caused once new TessBaseAPI(); is called.
dlopen("/data/app-lib/com.datumdroid.android.ocr.simple-2/liblept.so") failed: Cannot load library: soinfo_link_image(linker.cpp:1635): could not load library "libpng.so" needed by "liblept.so"; caused by load_library(linker.cpp:745): library "libpng.so" not found
Can anyone help with that?
I have followed suggestion of Dmitry Zaitsev & thannks to him , solved my problem also .
Please update your TessBaseAPI.java from tess-two library project as below :
static {
System.loadLibrary("png");
System.loadLibrary("lept");
System.loadLibrary("tess");
nativeClassInit();
}
Build tess-two project after updating these file . In my case I have build it using eclipse.
Hope it should solve your problem as well.
It seems like System.loadLibrary("png") call is missing in TessBaseAPI, therefore library can't be found.
Try to call System.loadLibrary("png") before calling new TessBaseAPI(). Typically this is done in static initialization block, like so:
public class MyClass {
static {
System.loadLibrary("png");
}
public void doStuff() {
new TessBaseAPI();
}
}

Local notification plugin broken after cordova upgrade

I was using this plugin for cordova local notification.
Since cordova 5.0.0 with android#4.0.0 was released it does not work anymore.
I found the bug here.
Is there a way to temporarily solve it?
Thanks
Quick fix is to modify the block starting at LocalNotification:492 with the following:
webView.getView().post(new Runnable(){
public void run(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.sendJavascript(js);
} else {
webView.loadUrl("javascript:" + js);
}
}
});
i did a quick solution https://github.com/ivanhuay/cordova-plugin-local-notifications
try cordova plugin add https://github.com/ivanhuay/cordova-plugin-local-notifications#build_android_solution
i still have problems in some projects: UNEXPECTED TOP-LEVEL EXCEPTION
but it work for me in a new project

setup and write demo test the robolectric in Android Studio

Do anyone know how to integrate robolectric into android studio?
How to write sample test?
How to launch it?
I am working with android studio not to long, and I am too bad with gradle.
Searching the net didn't give me a result - I even could not launch official demo - https://github.com/robolectric/robolectric-samples . My android studio do not saw the test class.
Please give me simpliest step by step gide, thanks
Since robolectric runs in a JVM (i.e. not on a device or emulator), it is just a library and adding the test runner is all that's needed.
Make sure that the android SDK is later in the classpath than robolectric or junit - otherwise you'll get the stubbed methods from the android SDK.
#RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
#Test
public void shouldHaveApplicationName() throws Exception {
String appName = new MyActivity().getResources().getString(R.string.app_name);
assertThat(appName, equalTo("MyActivity"));
}
}
See http://robolectric.org/quick-start/

cordova 1.6.1 android Uncaught TypeError: Cannot call method 'showWebPage' of undefined

i just upgraded phonegap 1.4.1 to 1.6.1 and also upgraded the child browser from https://github.com/libbybaldwin/phonegap-plugins/tree/master/Android/ChildBrowser
and i have added this line in plugins.xml file
<plugin name="ChildBrowser" value="com.phonegap.plugins.ChildBrowser.ChildBrowser"/>
but when i calling this
window.plugins.childBrowser.showWebPage( "http://google.com", {
showLocationBar: true
});
i am geting this error Uncaught TypeError: Cannot call method 'showWebPage' of undefined
Concerning the 1.4.1 vs 1.6.1:
Please be aware that my github repo of phonegap-plugins is what I "froze" in internet time to allow continued use of phonegap 1.4.1 with plugins from that era.
Moving forward to cordova 1.6.1+ you'll need to move on the the official unofficial plugins at
https://github.com/phonegap/phonegap-plugins
Note: These plugins may or may not be updated work with cordova 1.6.1. Please read their individual README's and the blog below from a phonegap engineer:
http://simonmacdonald.blogspot.com/2012/04/migrating-your-phonegap-plugins-to.html
Summary: moving from 1.4.1 to 1.6.1+ and using phonegap plugins will require some vigilance as there were many changes internally between those two versions.
I was able to solve this error by using this code:
try {
var cb = new ChildBrowser();
console.log(cb);
cb.showWebPage('http://www.google.com');
}catch (err){
console.log(err);
}
Although this caused another error:
PhoneGap Build + Cordova + ChildBrowser Error
If you don't get any errors and this solution works - please let me know!
Have been fighting this same error for some days now and found that you can not call the childBrowser right away from your index.html, likely because the initialization of the childBrowser plugin takes some time to complete.
This solution works:
setTimeout(function() {
window.console.log('Opening Childbrowser...');
window.plugins.childBrowser.showWebPage("http://www.google.com", { showLocationBar: true });
}, 3500);
In other words: you have to wait for the childBrowser object to become available, there are more elegant solutions thinkable of course.
DISCLAIMER: tested with the current 1.7.0rc1 Cordaova release, not sure if this works for 1.6.1 as well.

Categories

Resources