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
Related
I'm trying to implement a chat app using stfalcon's ChatKit library. I've followed the docs in their repo, but there are things I'm not sure I'm getting right.
First, I created a new activity called DialogsListActivity, and copied the xml in the activity's xml file.
From here I first copied the xml part to the activity's xml file.
Next comes the adapter setup. I copied the given code after the OnCreate method, including the last line (dialogsListView.setAdapter(dialogsListAdapter);) as the last line in OnCreate. The whole activity now looks like this:
ListView dialogsListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialogs_list);
dialogsListView.setAdapter(dialogsListAdapter);
}
DialogsListAdapter dialogsListAdapter = new DialogsListAdapter<>(dialogs, new ImageLoader() {
#Override
public void loadImage(ImageView imageView, String url) {
//If you using another library - write here your way to load image
Picasso.with(DialogsListActivity.this).load(url).into(imageView);
}
});
}
Questions:
is this the right place to put the adapter in?
is it ok to put set the dialogsListView as an attribute and defining it inside OnCreate()?
the dialogs from new DialogsListAdapter gets a Cannot resolve symbol 'dialogs' message.
the new ImageLoader() from same place gets a Class 'Anonymous class derived from ImageLoader' must either be declared abstract or implement abstract method 'loadImage(ImageView, String, Object)' in 'ImageLoader'
What am I missing there?
For the IDialog and IUser implementation I created the classes DefaultDialog and Author, and copied the given code. As I expected, the 'symbols' returned by the methods 'cannot be defined'. Where should they be defined and how?
Next in the tutorial is the Data management section which I think would set those values.
I already downloaded the sample project and tried to look inside, but I cannot find the public class DefaultDialog that implements IDialog or anything similar. Plus, I got pretty lost trying to understand the library from that sample project.
Any help would be much appreciated.
Thanks in advance.
I am developing an android app, and I am trying to implement HorizontalScrollView on almost all the activities in the app. (almost 50+).
I am looking to define a class/variable/function in the java file, then call it on the XML file, so that I don't have to retype/copy and paste the code more than 50 times.
Also, if I define a function in my MainActivity, is it possible to use it in other java/XML files?
In xml file you can include other xml layout. I think you can use it.
First of all, you should only have ONE Activity in your app as an entry point and use Fragments for different windows, this is an idea which Google is pushing at the moment.
Second of all, YES, you can reuse MainActivity function in other java files, but not in XML files. Example:
Define an interface:
public interface MyInterface {
void triggerMainActivityFunction();
}
Then implement this interface in your MainActivity:
public class MainActivity extends Activity implements MyInterface {
...
#Override
public void triggerMainActivityFunction() {
// Do something
}
...
}
Define listener in all of the java classes where you want to trigger this function:
public class SomeClass extends Fragment {
...
private MyInterface listener;
#Override
void onCreate(...) {
listener = (MyInterface) getActivity();
}
// your function to trigger a reusable
// function from Activity when user clicks on something
public void onClick() {
listener.triggerMainActivityFunction();
}
...
}
Third, YES, you can reuse XML layouts by using include tag.
Hope this helps. Good luck :)
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>
I have an application class and it holds a reference of MyAdapter class:
public class MyApplication extends Application {
......
private static MyAdapter sMyAdapter;
public static MyAdapter getMyAdapter() {
if (sMyAdapter == null) {
sMyAdapter = new MyAdapter(this);
MyApplication.setMyAdapter(sMyAdapter);
}
return sMyAdapter;
}
public static void setMyAdapter(MyAdapter myAdapter) {
sMyAdapter = myAdapter;
}
......
}
MyAdapter class is a customized android adapter class, and the application Context is passed to the Adapter. The application holds a reference of it because it may be used anytime till the application is still running.
The problem is that now I need an Activity Context in the Adapter to start another Activity when some button is clicked because if I use application Context I need to add a Intent flag FLAG_ACTIVITY_NEW_TASK which I don't want to because that way the new Activity being started will be running in a new task. I tried a lot with changing launch mode and taskAffinity but either new issues came up or the Activity will be running in a new task.
So I am thinking to hold an Activity reference which shows the button in the Adapter class, and to avoid memory leak, I came up with the following:
public class MyActivity extends Activity {
......
#override
public void onResume() {
......
MyApplication.getMyAdapter().setActivity(this);
......
}
......
#override
public void onDestroy() {
......
MyApplication.getMyAdapter().setActivity(null);
......
}
}
Then in the Adapter class I will use the Activity reference to start another Activity. I tested and this worked fine but the question is would this avoid memory leak and is this a proper way to hold the Activity reference when onResume and release it when onDestroy? Is there any other decent way to achieve my purpose? Thanks.
would this avoid memory leak
Not really. You MyApplication object will still keep a reference to your adapter with all it's 'contents' (further references).
Yes, you've got rid of keeping the destroyed Activity around, and you might feel it's okay to keep the adapter because you're 'going to need it again anyways', still this whole construct is a horrible code smell and will with certainty introduce new memory leaks and other problems as you develop this further.
Logically, this adapter is part of an Activity, and as the Activity 'dies', so should the adapter.
I'm sure there's a reason why you felt you needed that adapter on your application, so I'd post another question asking 'how can I achieve soandso without my application knowing of my adapter'.
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..