チェス盤のような 36 個のボタン (6X6) を持つアクティビティがあり、そのうちの 1 つをクリックすると、別のクラスが呼び出され、質問が表示されます。方法は次のとおりです(最初のボタンの例):
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;
次に、質問がポップアップします (Pitanja_Cigle.class)。
120 秒後に時間切れになり、別のポップアップ クラス (Popup_opis.class) があり、ユーザーに時間切れとその他の情報を伝えますが、これには関係ありません。
問題は、ゲームの最後の数秒で、36 個のボタンのいずれかをクリックして質問がポップアップし、時間切れになりましたが、時間内に質問に答えることができず、Popup_opis.class がポップアップすることです。 OK をクリックして閉じると、最後の質問がまだ開いていることがわかります。時間切れになった瞬間にその質問アクティビティを終了/終了するにはどうすればよいですか?
ここに私の 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
}
}
そして私の質問クラス 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);
}
}
スタックトレース:
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