我有一个带有 36 个按钮(6X6)的活动,比如棋盘,当我单击其中一个按钮时,我会调用另一个弹出问题的类。以下是方法(第一个按钮的示例):
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,当我点击确定关闭它时,我可以看到最后一个问题仍然打开。时间一到,如何杀死/完成该问题活动?
这是我的 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