Android Interface Implementation Error for Network Fragment - java

I wanted to make a network fragment on my Android app so I could upload and download information from my database server. Following the guide on Android networking on the Developer page and the corresponding example project on Github, I created a demo to test a network connection.
I copied the files DowloadCallback.java (contains the implemented network interface) and NetworkFragment.java (the network fragment thread) word for word from the example project and added the necessary permissions in AndroidManifest.xml.
When I tried to implement the fragment into my activity, I got errors in several rather odd and counterintuitive places:
Code
public class MainActivity extends FragmentActivity implements DownloadCallback {
...
#Override
public void updateFromDownload(String result) {
...
}
}
Errors
Class 'MainActivity' must either be declared in abstract or implement method 'updateFromDownload(T)' in 'DownloadCallback'
Method does not override method from its superclass
The public class says it needs a particular method for the class to implement DownloadCallback, but when I add such method it says that it does not exist in its superclass. How can these errors coexist? How can I fix this?
By the way, this is the exact same way the main activity class is defined in the sample project. Also I have posted this as an issue on Github but I am hoping to get a quicker response and attention here.

base on this You have to define <T> for DownloadCallback
In your case T is String
So change your code like below
public class MainActivity extends FragmentActivity implements DownloadCallback<String>

Related

Should I be worried about lint Warnings "Method X must be called from the worker thread.." on LiveData?

I have built an Android Java MVVM app, based on some google codelabs no longer published.
But to demonstrate my issue, I found that Google currently has 'Room With a View' app https://github.com/googlecodelabs/android-room-with-a-view, so I cloned it and it exhibits the issue I am experiencing.
In the Activity or Fragment, google suggest to Observe Livedata with a construct like:
public class MainActivity extends AppCompatActivity {
...
protected void onCreate(Bundle savedInstanceState) {
...
mWordViewModel.getAllWords().observe(this, words -> {
// Update the cached copy of the words in the adapter.
adapter.submitList(words);
});
}
}
Which is communicating with a ViewModel :
public class WordViewModel extends AndroidViewModel {
....
LiveData<List<Word>> getAllWords() {
return mAllWords;
}
}
And all works nicely.
However, if I annotate the Activity with #UiThread, and the ViewModel with #WorkerThread, most of my observe methods, asuch as the one above in MainAvtivity, I am told (by lint):
Method getAllWords must be called from the worker thread, currently inferred thread is main thread
Inspection info:Ensures that a method which expects to be called on a specific thread, is actually called from that thread. For example, calls on methods in widgets should always be made on the UI thread.
I see three options:
My Observes and VM are badly constructed and need re-work
I can ignore Lint's warning in this case
I have erred in useing #UiThread on MainActivity and #WorkerThread on ViewModel?
Happy to hear which it is. First SO question, so apologies if I have fumbled in my submission.
I tried an Observe on Livedata as listed above, and I annotated MainActivity #UiThread and ViewModel #WorkerThread. I expected to not see a lint warning on the Observes.

Override not working on onNewTask() in FirebaseMessagingService

I've just installed Firebase in my Android app to enable push notifications. I confirmed it's working by sending a push notification from the Firebase console to all devices running the app. Now I'm trying to get the FCM token (the unique identifier of the app instance) so I can send messages to specific devices. However, when I override the onNewToken() function in my class extending the FirebaseMessagingService, the method is not overridden. According to the docs it should work. The other methods in the doc are successfully overridden. I've found and decompiled the FirebaseMessagingService class file in my external libraries, and found out that the onNewToken function is indeed not present there.
How can I override a function that should exist according to the docs, but doesn't exist in the class file?
I'm using com.google.firebase:firebase-messaging:17.3.0 and com.google.firebase:firebase-core:16.0.0
The reason is because, as you comment before, Firebase Instance ID is deprecated but it refers to the complet class, then you should use #Override ... onNewToken in a different class, not in Firebase Instance Id class .Sorry for my english. The example is this:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onNewToken(String s) {
Log.e("NEW_TOKEN", s);
}

How does an interface communicate - Need a detailed answer

I have a fragment class:
public class UploadFragmentOne extends Fragment {}
I have subclassed:
public interface Communicator {
void communicate(int position);
}
In the onCreateView:
((Communicator) getActivity()).communicate(1);
The hosting activity signature:
public class DetailsPager extends FragmentActivity implements UploadFragmentOne.Communicator {}
Member function in the above activity:
public void communicate(int position) {
Toast.makeText(DetailsPager.this, "Clicked " + position, Toast.LENGTH_LONG).show();
}
This works like a charm, but I dont understand HOW? Sorry this might be too dumb, but I want to know how the control flows in this?
You should have a look on the following link once.-
Now come to your query
(MyActivity)activity.some_method()
Now suppose you are going to attach the same fragment for another activity say MainActivity then you need to do like -
(MainActivity)activity.some_method()
Now suppose some other activity are implementing the same fragment then each time you need to use the instanceOf check and then call the method and also add it.
Again suppose a trivial case when you are going the create a library and you are going to offer some result delivery for the particular event then?
How will you get the instance type ?
interface as the name suggests offer a way to be communicated without the actual class instance. They only need to be implemented. You can have a look on the OnClickListener in the View class of android API sources.

android phonegap cordova webview events

In my code, I am properly using webview and handling onPageFinished and shouldOverrideUrlLoading events. Now I redefined it as DroidGap in order to handle its functions. I should then continue intercepting these events because if I override it, phonegap functions are not executed anymore (as I do on iOS)! how to embed it and handle described events? thank you
public class webPush extends Activity implements CordovaInterface{
You don't need to redefine it to DroidGap. You can use your own implementation using CordovaInterface and just copy the functions you need from DroidGap java class. That way you can edit existing code or add your own.
This is what the official PhoneGap documentation says:
Modify your activity so that it implements the CordovaInterface. It is recommended that you implement the methods that are included. You may wish to copy the methods from /framework/src/org/apache/cordova/DroidGap.java, or you may wish to implement your own methods
Embedding Cordova WebView on Android
Embedding Webview we can do like this way basically
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");
}
If still you are getting the error ,kindly post your code along with logcat output,It will be easy to resolve the issue..

calling static methods from other classes

so i'm having some problems integrating scoreloop into my game. I use cocos2dx which is written in c++ and uses the ndk. The main application class is derived from activity and not from android.app.application. Adding a button to the layout and using it to bring up a scoreboard or submit a score works,but it doesn't connect to the internet. i've found a solution to this here : scoreloop support forum or more specifically
Yes, using libgdx seems to be the issue. libgdx brings it's own Application class that is actually derived from Android's Activity, not Application. The helloworld sample from libgdx does not come with an (Android) Application class at all, here is how to add one:
Create a new class that extends android.app.Application (not com.badlogic.gdx.backends.android.AndroidApplication)
In the AndroidManifest.xml find the tag and the name of the created class as an attribute: android:name="YourApplication"
Add the method public void onCreate() to that class and initialize Scoreloop there.
so following that i created this :
public class scoreLooped extends android.app.Application{
public void onCreate(Bundle savedInstanceState){
ScoreloopManagerSingleton.init(this, "redacted");
}
public void onTerminate()
{
ScoreloopManagerSingleton.destroy();
}
}
and i create this class from my main activity class like this :
public class wordsweeper extends Cocos2dxActivity implements OnScoreSubmitObserver{
private Cocos2dxGLSurfaceView mGLView;
private static scoreLooped a;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
wordsweeper.a = new scoreLooped();
//Set the observer equal to an instance of this class
ScoreloopManagerSingleton.get().setOnScoreSubmitObserver(this);
and the last line is where it crashes with an error "Caused by: java.lang.IllegalStateException: ScoreloopManagerSingleton.init() must be called first" so obviously my scoreLooped class doesn't call the scoreloopmanager. I've thought about using the scorelooped class to submit,retrieve scoreboards but it seems that i can't do that without extending activity. I'm pretty new to java so i might be missing something obvious so it would be great if somebody could point me in the right direction.
It seems that you need to create your own Application class and call ScoreloopManagerSingleton.init() there. See the answer here, which references some example documentation: http://support.scoreloop.com/discussions/problems/789-illegalstateexception-scoreloopmanagersingletoninit-can-be-called-only-once

Categories

Resources