Reputation: 1042
I have an activity with 36 buttons (6X6) like a chess board, and when I click one of them I call another class which pops up a question. Here's how (an example for first button):
public void onClick(View v) {
switch(v.getId()){
case R.id.ib1:
Intent i = new Intent(Cigle.this, Pitanja_Cigle.class);
startActivityForResult(i, B1);
break;
Then, the question pops up (Pitanja_Cigle.class).
After 120 seconds the time is up, and I have another popup class (Popup_opis.class) that tells the user that the time is up and some other info, it's not relevant to this.
The problem is, when, in the last seconds of the game I click one of 36 buttons and a question pops up, and the time is up, but I did not manage to answer the question on time, the Popup_opis.class pops up, and when I click OK in it to close it, I can see that last question still opened. How to kill/finish that question activity the moment the time is up?
Here's my Popup_opis.class:
public class Popup_opis extends Activity implements OnClickListener{
TextView tvOpis,tvNaslov,tvBrojPoena, tvResenje;
String primljenOpis, naslov, resenje;
int brojPoenaPrimljeno;
Button OK;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.popup_opis);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
primljenOpis = extras.getString("poslatOpis");
brojPoenaPrimljeno = getIntent().getIntExtra("brojPoenaPrimljeno", 0);
naslov = extras.getString("naslov");
resenje = extras.getString("resenje");
}
initVariables();
}
private void initVariables() {
Typeface tv = Typeface.createFromAsset(getAssets(), "ARIALN.TTF");
OK = (Button) findViewById(R.id.bOK);
tvOpis = (TextView) findViewById(R.id.tvOpis);
tvBrojPoena = (TextView) findViewById(R.id.tvBrojPoena);
tvBrojPoena.setTypeface(tv);
tvNaslov = (TextView) findViewById(R.id.tvNaslov);
tvNaslov.setTypeface(tv);
tvResenje = (TextView) findViewById(R.id.tvResenje);
tvResenje.setTypeface(tv);
tvOpis.setTypeface(tv);
tvOpis.setText(primljenOpis);
tvBrojPoena.setText("Osvojili ste " + brojPoenaPrimljeno + " poena u ovoj igri.");
tvNaslov.setText(naslov);
tvResenje.setText(resenje);
OK.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
public void onBackPressed() {
super.onBackPressed();
finish();
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
And my questions class, Pitanja_cigle.class:
public class Pitanja_Cigle extends Activity{
public static String tacanOdg;
int counter = 0;
Button b1, b2, b3, b4;
TextView question;
boolean tacno = true;
boolean pogresno = false;
LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();
private String generateWhereClause(){
StringBuilder result = new StringBuilder();
for (Long l : mAnsweredQuestions){
result.append(" AND _ID <> " + l);
}
return result.toString();
}
Runnable mLaunchTaskFinish = new Runnable() {
public void run() {
finish();
}
};
private class Answer {
public Answer(String opt, boolean correct) {
option = opt;
isCorrect = correct;
}
String option;
boolean isCorrect;
}
Handler mHandler = new Handler();
final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
Answer ans = (Answer) v.getTag();
if (ans.isCorrect) {
Intent resp = new Intent();
resp.putExtra("score", tacno);
setResult(1, resp);
Intent i = new Intent("rs.androidaplikacije.toplo_hladno.TACANODGOVOR");
startActivity(i);
mHandler.postDelayed(mLaunchTaskFinish,1200);
}
else{
Intent resp = new Intent();
resp.putExtra("score", pogresno);
setResult(1, resp);
Intent i = new Intent(getApplicationContext(), PogresanOdgovor.class);
i.putExtra("tacanOdgovor", tacanOdg);
startActivity(i);
mHandler.postDelayed(mLaunchTaskFinish,2200);
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.pitanja_cigle);
InicirajVariable();
nextQuestion();
}
private void nextQuestion() {
counter++;
TestAdapter mDbHelper = new TestAdapter(this);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
if(!myDbHelper.checkDataBase()){
mDbHelper.createDatabase();
}
try{ //Pokusava da otvori db
mDbHelper.open(); //baza otvorena
Cursor c = mDbHelper.getPitanjaCigle(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
List<Answer> labels = new ArrayList<Answer>();
labels.add(new Answer(c.getString(2), true));
labels.add(new Answer(c.getString(3), false));
labels.add(new Answer(c.getString(4), false));
labels.add(new Answer(c.getString(5), false));
tacanOdg = c.getString(2);
Collections.shuffle(labels);
question.setText(c.getString(1));
b1.setText(labels.get(0).option);
b1.setTag(labels.get(0));
b1.setOnClickListener(clickListener);
b2.setText(labels.get(1).option);
b2.setTag(labels.get(1));
b2.setOnClickListener(clickListener);
b3.setText(labels.get(2).option);
b3.setTag(labels.get(2));
b3.setOnClickListener(clickListener);
b4.setText(labels.get(3).option);
b4.setTag(labels.get(3));
b4.setOnClickListener(clickListener);
}
finally{ // kada zavrsi sa koriscenjem baze podataka, zatvara db
mDbHelper.close();
}
}
private void InicirajVariable() {
Typeface pitanje = Typeface.createFromAsset(getAssets(), "myriad.ttf");
Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
question = (TextView) findViewById(R.id.tvPitanjeCigle);
b1 = (Button) findViewById(R.id.bOdgCigle1);
b2 = (Button) findViewById(R.id.bOdgCigle2);
b3 = (Button) findViewById(R.id.bOdgCigle3);
b4 = (Button) findViewById(R.id.bOdgCigle4);
b1.setTypeface(dugmad);
b2.setTypeface(dugmad);
b3.setTypeface(dugmad);
b4.setTypeface(dugmad);
question.setTypeface(pitanje);
}
}
Stack trace:
06-07 14:16:23.631: E/AndroidRuntime(5523): FATAL EXCEPTION: main
06-07 14:16:23.631: E/AndroidRuntime(5523): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=30, result=0, data=null} to activity {rs.androidaplikacije.toplo_hladno/rs.androidaplikacije.toplo_hladno.Cigle}: java.lang.NullPointerException
06-07 14:16:23.631: E/AndroidRuntime(5523): at android.app.ActivityThread.deliverResults(ActivityThread.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at android.app.ActivityThread.handleSendResult(ActivityThread.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at android.app.ActivityThread.access$2000(ActivityThread.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at android.app.ActivityThread$H.handleMessage(ActivityThread.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 14:16:23.631: E/AndroidRuntime(5523): at android.os.Looper.loop(Looper.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at android.app.ActivityThread.main(ActivityThread.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 14:16:23.631: E/AndroidRuntime(5523): at java.lang.reflect.Method.invoke(Method.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at dalvik.system.NativeStart.main(Native Method)
06-07 14:16:23.631: E/AndroidRuntime(5523): Caused by: java.lang.NullPointerException
06-07 14:16:23.631: E/AndroidRuntime(5523): at rs.androidaplikacije.toplo_hladno.Cigle.onActivityResult(Cigle.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at android.app.Activity.dispatchActivityResult(Activity.)
06-07 14:16:23.631: E/AndroidRuntime(5523): at android.app.ActivityThread.deliverResults(ActivityThread.)
06-07 14:16:23.631: E/AndroidRuntime(5523): ... 11 more
Upvotes: 3
Views: 4299
Reputation: 3236
Register a broadcast receiver for your Pitanja_cigle's class onCreate method.
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action= intent.getStringExtra("action");
if(action.equals("close")) {
Pitanja_cigle.this.finish();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter("Pitanja_cigle"));
}
Whenever you want to close it, do
public static void closePitanja(Context context) {
Intent intent = new Intent("Pitanja_cigle");
intent.putExtra("action", "close");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
Upvotes: 5
Reputation: 5806
My suggestion is to call finish()
on the Pitanja_cigle
Activity at the same time as when the OK button is pressed.
OK.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Pitanja_cigle.this.finish();
finish();
}
});
Upvotes: 0