我有一个像棋盘一样的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弹出,当我点击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
答案 0 :(得分:5)
为您的Pitanja_cigle课程onCreate方法注册广播接收器。
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"));
}
每当你要关闭它时,请执行
public static void closePitanja(Context context) {
Intent intent = new Intent("Pitanja_cigle");
intent.putExtra("action", "close");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
答案 1 :(得分:0)
我的建议是在按下确定按钮的同时在finish()
活动上拨打Pitanja_cigle
。
OK.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Pitanja_cigle.this.finish();
finish();
}
});