gpt4 book ai didi

java - 如何从另一个 Activity 中杀死/完成和 Activity ?

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:46 26 4
gpt4 key购买 nike

我有一个像棋盘一样有 36 个按钮 (6X6) 的 Activity ,当我单击其中一个按钮时,我会调用另一个类,它会弹出一个问题。方法如下(第一个按钮的示例):

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弹出,当我在其中单击确定关闭它时,我可以看到最后一个问题仍然打开。如何在时间到了时终止/完成该问题 Activity ?

这是我的 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

最佳答案

为您的 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);
}

关于java - 如何从另一个 Activity 中杀死/完成和 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16974433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com