Cant get the location and address of the user - java

[So i have that problem that it gives me an error like:
error: incompatible types: <anonymous LocationCallback> cannot be converted to PendingIntent
LocationServices.getFusedLocationProviderClient(getActivity()).requestLocationUpdates(locationRequest, new LocationCallback() {
So how can i get over it ? I want to find the address of the user and update it. I have also fragments that i have to write "this" instead of getActivity() but it gives an error each way.]1

In the photo attached the error is shown because you have not rewritten the code completely you are missing the 3rd parameter looper
your code should be something like
LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
//All your code for parsing or extracting data
}
},
Looper.myLooper());

Related

The app back to previous page when I attempt to fetch data from another activity

I have 1 adapter file, contain following code:
//When user click detailBtn, it will pass the data to EventDetail page.
holder.detailBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(holder.detailBtn.getContext(), EventDetail.class);
intent.putExtra("eventName", datalist.get(position).getEventName());
intent.putExtra("eventDate", datalist.get(position).getEventDate());
intent.putExtra("eventVenue", datalist.get(position).getEventVenue());
intent.putExtra("eventDesc", datalist.get(position).getEventDesc());
intent.putExtra("eventRole", datalist.get(position).getEventRoles());
intent.putExtra("eventCreator", datalist.get(position).getEventCreator());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
holder.detailBtn.getContext().startActivity(intent);
}
});
The main problem occur here, following code is caused the problem::
So, when I try to access the field "eventRole" and put it in ArrayAdapter in EventDetail java file using following code:
String[] roles = (String[]) getIntent().getStringArrayExtra("eventRole");
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.option_item, roles);
The app jump back to previous page :(
Can anyone help me to look into why it return back to previous activity when I try to access it? I can't find any clue on the logcat it shows to me...
The "eventRole" array is stored as Map Structure in Firestore.
Are you sure that your getEventRoles() method return String[] ? As far as I understood, you have casted getIntent().getStringArrayExtra("eventRole"); to String[] even if getStringArrayExtra() is returning String[], Why ?
I think the problem is in your getEventRoles() method, it might not be returning String[] and you are trying to cast that incompatible type to String[] which is throwing ClassCastException.

onStart and Override Error in android studio

I wrote some code to get data from Firebase, but it shows some errors. I will explain the errors below and attach a picture.
First error when I put the mouse on it, message show:
'onStart()' in 'com.abdullrahman.eng.myapp.FriendsFragment' clashes with 'onStart()' in 'android.app.Fragment'; attempting to assign weaker access privileges ('protected'); was 'public'
Second error when I put the mouse on it, message show:
Method does not override method from its superclass
Can anyone solve these problems and help me?
The code:
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Friends, FriendsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(
Friends.class,
R.layout.users_single_layout,
FriendsViewHolder.class,
mUsersDatabase
) {
#Override
protected void populateViewHolder(FriendsViewHolder friendsViewHolder, Friends friends, int position) {
friendsViewHolder.setDisplayName(friends.getName());
friendsViewHolder.setUserStatus(friends.getStatus());
friendsViewHolder.setUserImage(friends.getThumb_image(), mMainView.getContext());
final String user_id = getRef(position).getKey();
friendsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent profileIntent = new Intent(getActivity(), UserProfile.class);
profileIntent.putExtra("user_id", user_id);
startActivity(profileIntent);
}
});
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
}
Errors do say a lot about the problem you have.
The first error is caused by protected access modifier on the onStart() method, while it should be public.
It should be
#Override
public void onStart() {
super.onStart();
// rest of the code
}
instead of
#Override
protected void onStart() {
super.onStart();
// rest of the code
}
You can find some more information about the error reason in docs available on Oracle site.
The second problem is related to the definition of FirebaseRecyclerAdapter. Looks like there is no method like
protected void populateViewHolder(FriendsViewHolder friendsViewHolder, Friends friends, int position).
I'd suggest checking docs/sources of this class to get info about how the override method should look for the version of Firebase you are using in your project.
Also, as I can see you are using IntelliJ IDEA or some similar IDE, so you can use built-in feature to implement/override the correct method.
Change your protected modifier to public and it'll go away.
In Java, the overriding method can have the same or weaker access compared to the parent method... Here the parent class declares the method as public so you have no other choice but to override it with a public access modifier as it's the weakest access possible.

Can't resolve makeToast

EDIT :
*The entire issue was that since i miss-read .makeText for .makeToast, since
i have corrected this i have found that both of the previous attempts actually work for my app. *
I'm having issues implementing Toast.makeToast method in my android app :
I tried
public void onRightCardExit(Object dataObject) {
Toast.makeToast(MainActivity.this, "Right", Toast.LENGTH_SHORT).show();
}
as well as
public void onLeftCardExit(Object dataObject) {
Toast.makeToast(getApplicationContext(), "Left", Toast.LENGTH_SHORT).show();
}
On the first one i get the issue
"Can not resolve method 'makeToast.(android.Content.Context,
java.lang.String, int)' "
On the second one a similiar but just more specific pointer to my java file for the context
"Can not resolve method
'makeToast.(com.sanruza.alpak.tinderlike.MainActivity,
java.lang.String, int)' "
I understand that the correct syntax is .makeToast( context, String, int ), but i still can't get it to work.
It should be makeText instead of makeToast
Toast.makeText(context,toastMsg, Toast.LENGTH_SHORT).show();
See the docs for more info..
Please read the Google Developers link: Developers Toast Document
This snapshot should clarify your concern:
makeToast does not exist, you must use makeText.
Toast.makeText(getApplicationContext(), "Msg", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Msg", Toast.LENGTH_LONG).show();

Google Fit Listen for Data Updates not working

I'm trying to implement a Google Fit Listener when data is updated into Google Fit services.
In this link of Google Fit documentation there is a simple example, however, it is not 100% clear. For that reason, I have two problems:
I don't know how to implement mResultCallback variable (there aren't any examples in this documentation).
When I define a simple ResultCallback (it seems to work but I'm not sure) and I launch the application, it gives me a result error code: java.lang.SecurityException: Signature check failed
The code within the HistortyApi lists one of android.permission.ACCESS_FINE_LOCATION or android.permission.BODY_SENSORS as being required.
Adding those permissions to my code hasn't resolved the same problem though.
Confirmed bug in Google Fit services. See discussion in https://plus.google.com/110141422948118561903/posts/Lqri4LVR7cD
mResultCallback is a ResultCallback<Status> so you need to implement a class of that type. Documentation is here, but there's only one method you need to implement:
public abstract void onResult (Status result)
The standard way is to do this using an anonymous class either when you declare mResultCallback or when you're using it as a parameter. Below is an example from Google's BasicRecordingAPI example:
Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_ACTIVITY_SAMPLE)
.setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
if (status.isSuccess()) {
if (status.getStatusCode()
== FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.i(TAG, "Existing subscription for activity detected.");
} else {
Log.i(TAG, "Successfully subscribed!");
}
} else {
Log.i(TAG, "There was a problem subscribing.");
}
}
});
If you want to use a member variable you can simply make an assignment instead:
ResultCallback<Status> mResultCallback = new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
...
}
});
Of course you can define a non-anonymous class, but if you did that for every callback you had you would end up creating a LOT of classes.

Strange Reflection Errors

I've been trying workarounds/dealing with this problem for weeks and I can't seem to get around it any longer. I have an Android Application that needs to change the device's name before creating a Peer to Peer network.
Because the method is hidden in Android's SDK, I am using reflection. The method I am trying to reflect is located here at line 1305: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/wifi/java/android/net/wifi/p2p/WifiP2pManager.java
Here is my code trying the reflection:
public class HostStagingActivity extends Activity
{
WifiP2pManager.Channel myChannel; //This channel is created and passed to system services in order for WIFI_P2P to work
WifiP2pManager myManager; //This manager is declared to get the required channel object
...
#Override
protected void onCreate(Bundle savedInstanceState)
{
myManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);//passed to the myReceiver listener class
myChannel = myManager.initialize(this, getMainLooper(), null);//passed to the myReceiver listener class
...
Method method1 = null;
try
{
method1 = myManager.getClass().getMethod("setDeviceName", new Class[] {WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class });
method1.invoke(myChannel, NETWORK_NAME, new WifiP2pManager.ActionListener()
{
public void onSuccess()
{
Toast.makeText(getApplicationContext(), "DeviceName Changed Successfully!", Toast.LENGTH_SHORT).show();
//Code for Success in changing name
}
public void onFailure(int reason)
{
Toast.makeText(getApplicationContext(), "DeviceName Change Returned Failure!", Toast.LENGTH_SHORT).show();
//Code to be done while name change Fails
}
});
}
However, this causes runtime error:
Caused by: java.lang.IllegalArgumentException: expected receiver of type android.net.wifi.p2p.WifiP2pManager, but got android.net.wifi.p2p.WifiP2pManager$Channel
at java.lang.reflect.Method.invokeNative(Native Method)
Why is the method expecting a WifiP2pManager object when I am clearly passing it a WifiP2pManager.Channel class in my code? Just for fun, when I pass it the arguements it expects, it claims the method was expecting three arguements and I only gave it two.
Can anybody with more reflection experience help me out?

Categories