i try to implement AppFlood advertising into my project. I do it with this tutorial . I got into the onCreate() method and then i get multiple errors. First i make imports and get warning The import com.appflood.AppFlood is never used. After this i tried to Initialize AppFlood Object and Splash screen. And after that i get synatx errors.
This is my MainActivity:
import com.appflood.AppFlood;
import com.appflood.AppFlood.AFEventDelegate;
import com.appflood.AppFlood.AFRequestDelegate;
public class onCreate();
AppFlood.initialize(this, "Your app key here", "Your secret key here", AppFlood.AD_ALL);
public class MainActivity extends AndroidApplication implements MyRequestHandler {
private GameHelper gamehelper;
private MyGdxGame mygdxgame;
I think you didn't read the document in full concentration. It said "InitializeAppFloodObject in your onCreate() method and before setContentView() is invoked ". Which means in your main activity there is an overridden method.
Example :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// place here
AppFlood.initialize(this, "Your app key here", "Your secret key here", AppFlood.AD_ALL);
setContentView(R.layout.activity_car_detail);
}
Read the rest of the tutorial carefully.
Edited : I pasted full code without error. Extend your class from activity.
public class Test extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// place here
AppFlood.initialize(this, "Your app key here", "Your secret key here", AppFlood.AD_ALL);
setContentView(R.layout.activity_car_detail);
}}
Related
I was developing an exam score calculator app.
When I want to call AD methods,advertisements don't show up.
Calculation process happens in OnCreate method:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
/*Calculation...*/}
and other voids like:
public void requestAd() {
/*AD RQUESTING PROCESS...*/
}
and
public void showAd() {
/*AD SHOWING PROCESS...*/
}
AD team gave me this code to call the method and it works well:
requestButton.setOnClickListener(v -> requestAd());
showButton.setOnClickListener(v -> showAd());
But the Problem is I don't have buttons to call them so I tried this:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
requestAd();
showAd();
/*Calculation...*/}
But when the activity starts ads don't show up!
The whole question is I want this methods to be called while this activity starts.
thank you.
Try building up the release version APK and test on it. Maybe your Ad-provider have some restrictions in debug version?
I made another class and moved request Ad and showAd there. Then, I made an object and called the method through object.
I have to mention that I changed a minor thing in requestAd but the main job was done by the object.
Thank You All.
I am a fairly new to android development, and I don't understant how the main class works on Android Studio.
I'm trying to make my app have the Crashlytics and Parse services but I'm not sure where to put them. Currently I have the code on the OnCreate method in the Login Class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
Parse.initialize(this, "CODE", "CODE");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
But I heard that this code should go on the Application Class... That's because the Application Class is always started and its basically the main Class for the app... I am not sure about this, please correct me if I'm wrong.
If that's the case, how can I access the Application Class and where should I put the code?
Just create a class which should extends the Application , Using this you can initialize the parse installation
public class DemoClass extends Application {
#Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
Parse.initialize(this, "CODE", "CODE");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
Copy this above code
When running the activity the first time, Parte initialization (which is inside the onCreate method) goes well:
Parse.enableLocalDatastore(this);
Parse.initialize(this, "...", "...");
Then, If I press the back button and enter again in the activity, I get an error:
java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`
Which I do not really understand why, because the Parse.enableLocalDatastore(this); is before Parse.initialize(this, "...", "...");.
Well, Ok. Then I tried to retrieve when the enableLocalDatastore has finihed, with Parse.isInitialized() method, but it is private, so I can't use it (as well as some others Parse variables I could use).
After some time, I found that If I call both methods inside a new Thread it works.
I'd like to know why the error happens and why It was solved with the Thread. Also, is there any better way to do it?
Follows the code (trimmed for the important parts):
public class RegisterActivity extends Activity {
Button linkParse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
linkParse = (Button)findViewById(R.id.linkparse);
linkParse.setOnClickListener(new LinkParse());
linkParse.setClickable(false);
try {
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
Parse.initialize(this, "...", "...");
} catch(Exception e){
Toast.makeText(RegisterActivity.this, "Parse not started.", Toast.LENGTH_SHORT).show();
linkParse.setClickable(true);
}
}
class LinkParse implements View.OnClickListener{
#Override
public void onClick(View v) {
Thread thread = new Thread(new StartParse());
thread.start();
}
}
class StartParse implements Runnable{
#Override
public void run() {
try {
// Enable Local Datastore.
Parse.enableLocalDatastore(RegisterActivity.this);
Parse.initialize(RegisterActivity.this, "...", "...");
} catch(Exception e){
}
}
}
}
You should invoke these two lines of codes from the application class not from the activity
Parse.initialize(this, "....","....");
Parse.enableLocalDatatore(this);
There is no need to initialized this multiple times and global application state is meant to be in the application class.
Create a class and extend application and initialize your parse code there
public class MyApplication extends Application{
#Override
public void onCreate(){
super.onCreate()
Parse.initialize(this, "....", "....");
Parse.enableLocalDatastore(this)
}
}
After you have created the application class. Add it to your AndroidManifest.xml
<application android:icon="#drawable/icon"
android:label="#stringapp_name"
android:name="MyApplication">
I got a problem in one Android Application which I am working in which I am getting error when I use PackageManager.
The Application is as follows:
Home.java
package com.main.home;
public class Home extends Activity
{
//onclicking a Button say "send"
send.onClickListener()
{
public void onClick() {
Intent i =new Intent();
i.setClassName("com.main.home", "com.main.home.home1");
startActivity(i);
}
}
Home1.java
package com.main.home;
import com.andr.resulting.Result ;
public class Home1 extends Activity {
EditText et=(EditText)findViewById(R.id.e1);
//Clicking on forwardButton in that onClick()
public void onClick()
{
String s[] = {e1.getText().toString(),""};
//Calling a method in a class Result of another package which is not activity
Result.finalfunc(Home1.this,s);
}
Result.java
package com.andr.resulting; //different package
public class Result {
public static void finalfunc(Activity act,String[] re) ...
//Here I want to get the details of this particular class's package (com.andr.resulting) using PackageManager
// I tried like this:
Result.this.getPackageManager().getApplicationInfo(Result.getPackageName(),0))
I am getting error getPackageManager() does not exists in this class file.
How do I solve this issue? I will be eagerly waiting for valuable reply.
Thanks in Advance.
try this::
this.getPackageManager().getApplicationInfo(this.getPackageName(), 0);
Result doesn't extend Context like your Activity class does. So the method isn't available in that class. You need to call act.getPackageManager() inside there instead of this.getPackageManager().
I got a problem in one Android Application which I am working in which I am getting error when I use PackageManager.
The Application is as follows:
Home.java
package com.main.home;
public class Home extends Activity
{
//onclicking a Button say "send"
send.onClickListener()
{
public void onClick() {
Intent i =new Intent();
i.setClassName("com.main.home", "com.main.home.home1");
startActivity(i);
}
}
Home1.java
package com.main.home;
import com.andr.resulting.Result ;
public class Home1 extends Activity {
EditText et=(EditText)findViewById(R.id.e1);
//Clicking on forwardButton in that onClick()
public void onClick()
{
String s[] = {e1.getText().toString(),""};
//Calling a method in a class Result of another package which is not activity
Result.finalfunc(Home1.this,s);
}
Result.java
package com.andr.resulting; //different package
public class Result {
public static void finalfunc(Activity act,String[] re) ...
//Here I want to get the details of this particular class's package (com.andr.resulting) using PackageManager
// I tried like this:
Result.this.getPackageManager().getApplicationInfo(Result.getPackageName(),0))
I am getting error getPackageManager() does not exists in this class file.
How do I solve this issue? I will be eagerly waiting for valuable reply.
Thanks in Advance.
try this::
this.getPackageManager().getApplicationInfo(this.getPackageName(), 0);
Result doesn't extend Context like your Activity class does. So the method isn't available in that class. You need to call act.getPackageManager() inside there instead of this.getPackageManager().