Android Question JavaObject 1.20 unknown member InitializeContext

ykucuk

Well-Known Member
Licensed User
Longtime User
Hello,

InitializeContext used almost all tutorial about java object or inline java.

unfortunally when i write this code to my project i got this error

NativeMe.InitializeContext ' unknown member InitializeContext

any help ?
 

ykucuk

Well-Known Member
Licensed User
Longtime User
I solved this problem.

Just delete javaObject from addtional lib folder.

current javaobject version is 2.01
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
i'm new member Mr Erel,

my java library is 2.02
and this is my code:
''''''''''''''''''''''''''''''''''''''''
Sub Process_Globals
Private nativeMe As JavaObject
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("x")
If FirstTime Then
nativeMe.InitializeContext
End If
End Sub



#If JAVA

import android.app.AlertDialog;


import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.content.DialogInterface;
import android.app.Activity;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.content.pm.PackageManager;
public void onClickWhatsApp() {

PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName,
ri.activityInfo.name));
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, element);

} catch (NameNotFoundException e) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

}
#end if
''''''''''''''''''''''''''''''''''''
and this is my error:
''''''''''''''''''''''''''''''''''''''''
B4A version: 5.02
Parsing code. (0.02s)
Compiling code. (0.03s)

ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code. (0.01s)
Generating R file. (0.10s)
Compiling generated Java code. Error
B4A line: 17
End Sub
javac 1.7.0_09
src\b4a\example\main.java:384: error: cannot find symbol
PackageManager pm = context.getPackageManager();
^
symbol: variable context
location: class main
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i'm new member
1. You should have started a NEW thread for your question.
2. This is a community forum. Don´t limit your question to a single member
3. Please use code tags when posting code.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
In your code
B4X:
...
public void onClickWhatsApp() {

  PackageManager pm = context.getPackageManager();
  //...
}

The error is that the var "context" isn't declared anywhere

You should do
B4X:
...
public void onClickWhatsApp(Context context) {

   PackageManager pm = context.getPackageManager();
   //...
}

and call it from B4A this way
B4X:
nativeMe.runMethod("onClickWhatsapp",Array(nativeMe))
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Mr DonManfred , i don't know from where to start new thread and how to make code tags, tell me please.

thank u JordiCP
i passed the prevouis error and i meet new one
''''''''''''''''''''''''''
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("x")
If FirstTime Then
nativeMe.InitializeContext
End If
nativeMe.runMethod("onClickWhatsapp",Array(nativeMe))
End Sub



#If JAVA

import android.app.AlertDialog;


import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.content.DialogInterface;
import android.app.Activity;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.content.pm.PackageManager;

public void onClickWhatsApp(Context context)
{
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName,
ri.activityInfo.name));
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, element);

} catch (NameNotFoundException e) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

}


#end if



Sub Button1_Down
nativeMe.runMethod("onClickWhatsapp",Array(nativeMe))
End Sub
''''''''''''''''''''''''''''''''''
error is
''''''''''''''''''''''''''''''''
B4A version: 5.02
Parsing code. (0.00s)
Compiling code. (0.06s)

ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code. (0.00s)
Generating R file. (0.15s)
Compiling generated Java code. Error
javac 1.7.0_09
src\b4a\example\main.java:391: error: cannot find symbol
intent.setComponent(new ComponentName(packageName,
^
symbol: class ComponentName
location: class main
Note: src\b4a\example\service1.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Do
To put something formatted as code, just put the text that you want to format with a starting [CODE ] and a final [/CODE ] but WITHOUT the final space between the "E" and the "]" (it has been added for obvious reasons)
Before clicking the "Post Reply" button, you can click the "Preview" button to see if you are doing it right​
Loop until you got it

To start a new thread, just click on "Android questions" (or the subforum you choose) in the general screen of the forum. Then, near the top-right you will see a button saying "Post New Thread"


About the error thrown, I think you made a bad copy/paste. These lines
B4X:
intent.setComponent(new ComponentName(packageName,
ri.activityInfo.name));
seem to be broken. It should be
B4X:
intent.setComponent(new ComponentName(packageName,Uri.activityInfo.name));

Also, you need to add , at the beginning
B4X:
import android.content.ComponentName;

...but the code you are trying to test has more errors ( "element" is not defined...)so it seems that you only got a part of a bigger java code. This way it will not work.

Please note that what you are trying yo do can be done easily only with b4A with no need of Inline Java.

To see if whatsapp is installed: HERE
And to send text: HERE
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
i see Mr JordiCP
but the code that you point me doesn't send the message directly ,it is just but the text on the whtsapp textbox message and just open the contacts tab for specified contact
,
the application i'm about to build is sms sender witch send sms to employees and if credit is finished it sends the messages to their whatsapp acount directly without human (programmaticly) .
,
i searched about that and they tell that it is possible in java and i'm good in vb but in java i still beginner,
i'll be so grateful if there is help to run one of the 10 solutions in the following link :
http://stackoverflow.com/questions/15462874/sending-message-through-whatsapp
,
thanks in advance every body
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Perhaps it is possible (to send directly to a contact without user interaction), but I tried similar things not much ago, and was not able to get it.
The API does not allow it, so if there is a working solution "now" it is not guaranteed to work in the future

Perhaps someone else knows more about it...
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
thank you very much Mr JordiCP for your reply, you really make me happy to join B4, it is my 1st day,
i will start it as thread and see my opportunities,
thanks again.
 
Upvote 0
Top