Hey im trying to add countdown timers to an arraylist but it is crashing. It is crashing once I try to add one countdowntimer . Was wondering is there anything I could do to fix it?
ArrayList<CountDownTimer> timers;
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
toggleLock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Checker = new CountDownTimer(1200000, 60000)
{
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
//does stuff
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
Checker.start();
timers.add(Checker);
}
}
});
}
I think you need to initialize the arraylist, so might be throwing null pointer exception.
timers=new ArrayList<CountDownTimer>();
You need to initialize your timers arraylist.
ex.
ArrayList<CountDownTimer> timers = new ArrayList<CountDownTimer>();
Related
I am trying to show flurry interstitial but getting following message in debug screen and I am not receiving interstitial on my screen.
07-14 15:55:31.390: W/webview(10588): java.lang.Throwable: Warning: A
WebView method was called on thread 'FlurryAgent'. All WebView methods
must be called on the UI thread. Future versions of WebView may not
support use on other threads.
I have followed this tutorial completely :
Android Integration
At present I am working on AndEngine. As error replying I put my all code in UI thread but result is same.
Here is my code for displaying ads :
protected void onCreate(Bundle pSavedInstanceState) {
super.onCreate(pSavedInstanceState);
// configure Flurry
FlurryAgent.setLogEnabled(false);
// init Flurry
FlurryAgent.init(MainGameActivity.this, MY_FLURRY_APIKEY);
mFlurryAdInterstitial = new FlurryAdInterstitial(MainGameActivity.this,
MY_ADSPACE_NAME);
FlurryAdTargeting adTargeting = new FlurryAdTargeting();
// enable test mode for this interstitial ad unit
adTargeting.setEnableTestAds(true);
mFlurryAdInterstitial.setTargeting(adTargeting);
// allow us to get callbacks for ad events
mFlurryAdInterstitial.setListener(interstitialAdListener);
mFlurryAdInterstitial.fetchAd();
}
FlurryAdInterstitialListener interstitialAdListener = new FlurryAdInterstitialListener() {
#Override
public void onFetched(final FlurryAdInterstitial adInterstitial) {
adInterstitial.displayAd();
}
#Override
public void onError(final FlurryAdInterstitial adInterstitial,
FlurryAdErrorType adErrorType, int errorCode) {
adInterstitial.destroy();
}
#Override
public void onAppExit(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onClicked(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onClose(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onDisplay(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onRendered(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onVideoCompleted(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
};
#Override
protected void onStart() {
super.onStart();
FlurryAgent.onStartSession(MainGameActivity.this);
}
#Override
protected void onStop() {
super.onStop();
FlurryAgent.onEndSession(MainGameActivity.this);
}
#Override
public void onDestroy() {
super.onDestroy();
mFlurryAdInterstitial.destroy();
}
So what can I do in this situation?
I am in a situation where I need to display the Rate dialog based on the following flow
Open the app (First time )--> display dialog every 2 minutes
If rated --> display dialog next month
If clicked on later button --> display dialog next week.
String rate_value=myPref.getString("rate_value", "later");
Log.e("rate", String.valueOf(rate_value));
if (rate_value=="later") {
initCalendarNextWeek();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
showRateDialog();
}
});
}
}, nextWeekDate);
}
else if (rate_value=="now") {
initCalendarNextMonth();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
showRateDialog();
}
});
}
}, nextMonthDate);
}
else if (rate_value=="no_thanks") {
myTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
showRateDialog();
}
});
}
}, 120000, 120000);
}
public void initCalendarNextMonth(){
cal=Calendar.getInstance();
cal_day=cal.get(Calendar.DATE);
cal_month=cal.get(Calendar.MONTH);
cal_year=cal.get(Calendar.YEAR);
nextMonthDate=new Date();
nextMonthDate.setDate(cal_day);
nextMonthDate.setMonth(cal_month+1);
nextMonthDate.setYear(cal_year);
}
public void initCalendarNextWeek(){
cal=Calendar.getInstance();
cal_day=cal.get(Calendar.DATE);
cal_month=cal.get(Calendar.MONTH);
cal_year=cal.get(Calendar.YEAR);
nextWeekDate=new Date();
nextWeekDate.setDate(cal_day+7);
nextWeekDate.setMonth(cal_month);
nextWeekDate.setYear(cal_year);
}
Ok, so maybe look at Calendar class. The set methods of Date are deprecated so instead of using
nextMonthDate = new Date()
rather maybe use
nextMonthDate = new Calendar();
because then you can use the methods stated in the Calendar documentation such as add instead of using the setDate or setMonth. I hope this helps.
I am integrating Game Circle SDK & Whisper Sync in my game. I have implemented the code, but it has caused an issue. When I load the game and get the game state from the WhisperSync and set my local variables, white patches are observed in some places randomly instead of the proper image. When I turn of GameCircle & Whisper Sync it runs fine.
My game is developed using Cocos2d Android.
Does any one encountered such issue?
I have attached the image for reference.
Some Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
CCGLSurfaceView _glSurfaceView = new CCGLSurfaceView(this);
setContentView(_glSurfaceView);
CCDirector.sharedDirector().attachInView(_glSurfaceView);
CCDirector.sharedDirector().setDisplayFPS(false);
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
CCScene scene = IntroLayer.scene();
CCDirector.sharedDirector().runWithScene(scene);
}
#Override
public void onResume() {
super.onResume();
if (IS_AMAZON) {
AmazonGamesClient.initialize(this, callback, myGameFeatures);
AmazonGamesClient.getWhispersyncClient()
.setWhispersyncEventListener(
new WhispersyncEventListener() {
#Override
public void onAlreadySynchronized() {
// TODO Auto-generated method stub
super.onAlreadySynchronized();
System.out
.println("FA here onAlreadySynchronized");
loadGameScene();
}
#Override
public void onDataUploadedToCloud() {
// TODO Auto-generated method stub
super.onDataUploadedToCloud();
System.out
.println("FA here onDataUploadedToCloud");
}
#Override
public void onDiskWriteComplete() {
// TODO Auto-generated method stub
super.onDiskWriteComplete();
System.out
.println("FA here onDiskWriteComplete");
}
#Override
public void onFirstSynchronize() {
// TODO Auto-generated method stub
super.onFirstSynchronize();
System.out
.println("FA here onFirstSynchronize");
loadGameScene();
}
#Override
public void onNewCloudData() {
// TODO Auto-generated method stub
super.onNewCloudData();
System.out
.println("FA here onNewCloudData");
}
#Override
public void onSyncFailed(FailReason reason) {
// TODO Auto-generated method stub
super.onSyncFailed(reason);
System.out
.println("FA here onSyncFailed reason: "
+ reason.name());
}
#Override
public void onThrottled() {
// TODO Auto-generated method stub
super.onThrottled();
System.out.println("FA here onThrottled");
}
});
Log.i(TAG, "onResume: call initiateGetUserIdRequest");
PurchasingManager.initiateGetUserIdRequest();
Log.i(TAG, "onResume: call initiateItemDataRequest for skus: "
+ LAppInfo.getInstance().getList());
Set<String> skus = new HashSet<String>(LAppInfo.getInstance()
.getList());
PurchasingManager.initiateItemDataRequest(skus);
}
CCDirector.sharedDirector().resume();
}
private AmazonGamesCallback callback = new AmazonGamesCallback() {
#Override
public void onServiceNotReady(AmazonGamesStatus status) {
// unable to use service
System.out.println("FA here callback onServiceNotReady: "
+ status.name());
}
#Override
public void onServiceReady(AmazonGamesClient amazonGamesClient) {
System.out.println("FA here callback onServiceReady: ");
agsClient = amazonGamesClient;
}
};
private void loadGameScene() {
LoadData();
CCScene mainMenu = LevelMenuScene.scene();
CCDirector.sharedDirector().replaceScene(
CCFadeTransition.transition(0.5f, mainMenu));
}
Solved it by a work around by having a boolean variable and set it to true after the data is loaded in firstsync/alreadysync method. and loading the scene in the main thread instead of the callback of whispersync. Will be glad to know if some one find a proper solution.
This question already has answers here:
Execute AsyncTask several times
(5 answers)
Closed 9 years ago.
I am new to Android and am now working on the counter thing using the AsyncTask.
So the thing is I'm having one button and with that button OnClickListener.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean check;
check=check_button_status();
if(check==true){
rec_forward_task.execute();
}
else
{
rec_forward_task.cancel();
}
}
});
So here the rec_forward_task is the class that extends the AsyncTask. The AsyncTask class is here.
//
private class CounterForwardTask extends AsyncTask<Void, Integer, Integer>
{
TextView record_counter_display;
int rec_counter,count;
int last_value;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
record_counter_display=(TextView) findViewById(R.id.rec_voice_counter);
rec_counter=0;
record_counter_display.setText(String.valueOf(rec_counter));
count=0;
}
public void cancel() {
// TODO Auto-generated method stub
onCancelled();
}
#Override
protected Integer doInBackground(Void... params) {
// TODO Auto-generated method stub
while(rec_status)
{
publishProgress(count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
last_value=count;
count=count+1;
}
return 1;
}
#Override
protected void onCancelled() {
// TODO Auto-generated method stub
record_counter_display.setText(String.valueOf(0));
}
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
record_counter_display.setText(String.valueOf(count));
}
});
}
#Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
record_counter_display.setText(String.valueOf(last_value));
}
}
I'm making the object for that class in the oncreate method only so now the question is.
When user press first time the counter is starting and displaying in the textview and again pressing that same button the counter progress is stop due to oncancel method is called.but if user again pressing the same button app force closing and exception that u can't start the task which is already started.so what will be the way to perform this kind of operation.Thanks for any reply.
if(check==true){
rec_forward_task = new CounterForwardTask();
rec_forward_task.execute();
}
else
{
rec_forward_task.cancel();
}
Instead of instantiating AsyncTask in onCreate instaniate it when you need to start it. Hope this helps.
You will have to create a new AsyncTask object.
AsyncTasks are meant to run only once.
Check this answer.
The async task is designed to run only once. But you can run it by creating new instances of the asynctask class. See the answer of Passionate Androiden
Threading rules
There are a few threading rules that must be followed for this class to work properly:
The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
The task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI thread.
Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
pls findout more in Developer site
I wish to remove the ad if they are clicked once.
I implemented the following code.
I also added android:launchMode="singleInstance"
in the manifest and android:alwaysRetainTaskState="true" in the activity section of the manifest.
But when i click on the ad and return to the app the ad is still showing .
My code is as follows.
public class MainActivity extends Activity implements AdListener{
adView = (AdView)findViewById(R.id.ad);
adView.loadAd(new AdRequest());
// my code
#Override
public void onDismissScreen(Ad arg0) {
if (adView != null) {
adView.destroy();
}
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}
adView.setAdListener(this); /* first you should add listener register */
and for dismiss screen
public void onDismissScreen(Ad p1)
{
yourlayout.removeView(adView); // your layout should me remove here
}