0

我目前正在使用 eclipse 开发一个 android 应用程序。我有一个在第二次调用的活动,它崩溃了。我认为问题是因为该活动的变量仍然具有值。当我们退出该活动时如何清除变量的值?

package com.example.anagramslayer;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class QuestionList extends Activity implements OnClickListener{

    private SQLiteAdapter mySQLiteAdapter;
    ImageView header;
    ImageView category;
    ImageButton homeModule;
    Button buttonQuestion1;
    Button buttonQuestion2;
    Button buttonQuestion3;
    Button buttonQuestion4;
    Button buttonQuestion5;
    Button buttonQuestion6;
    Button buttonQuestion7;
    Button buttonQuestion8;
    Button buttonQuestion9;
    Button buttonQuestion10;
    Button buttonQuestion11;
    Button buttonQuestion12;
    Button buttonQuestion13;
    Button buttonQuestion14;
    Button buttonQuestion15;

    //************** init String to get each of column index in database **************
    private List<String> getAnswer = new ArrayList<String>();
    private List<String>getQuestion = new ArrayList<String>();
    private List<String>getFlag= new ArrayList<String>();

    //************** init String to get each of column index in database, used in onClick************** 
    private List<String> getIdListener = new ArrayList<String>();
    private List<String> getFlagListener = new ArrayList<String>();

    //************** var used in onClick **************
    private long idListener1;
    private long idListener2;
    private long idListener3;
    private long idListener4;
    private long idListener5;
    private long idListener6;
    private long idListener7;
    private long idListener8;
    private long idListener9;
    private long idListener10;
    private long idListener11;
    private long idListener12;
    private long idListener13;
    private long idListener14;
    private long idListener15;
    private long flagListener1;
    private long flagListener2;
    private long flagListener3;
    private long flagListener4;
    private long flagListener5;
    private long flagListener6;
    private long flagListener7;
    private long flagListener8;
    private long flagListener9;
    private long flagListener10;
    private long flagListener11;
    private long flagListener12;
    private long flagListener13;
    private long flagListener14;
    private long flagListener15;
    private long getLevelKey;

    private String flag1;
    private String flag2;
    private String flag3;
    private String flag4;
    private String flag5;
    private String flag6;
    private String flag7;
    private String flag8;
    private String flag9;
    private String flag10;
    private String flag11;
    private String flag12;
    private String flag13;
    private String flag14;
    private String flag15;

    private String question1;
    private String question2;
    private String question3;
    private String question4;
    private String question5;
    private String question6;
    private String question7;
    private String question8;
    private String question9;
    private String question10;
    private String question11;
    private String question12;
    private String question13;
    private String question14;
    private String question15;

    private String answer1;
    private String answer2;
    private String answer3;
    private String answer4;
    private String answer5;
    private String answer6;
    private String answer7;
    private String answer8;
    private String answer9;
    private String answer10;
    private String answer11;
    private String answer12;
    private String answer13;
    private String answer14;
    private String answer15;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_list);

        //************** retrieved parameter from LevelList **************
        Bundle extras = getIntent().getExtras();
        getLevelKey = extras.getLong(this.getPackageName());

        //************** fetch data from database **************
        fetchData();

        //************** init button ************** 
        homeModule =  (ImageButton) findViewById(R.id.home_button);
        buttonQuestion1 = (Button) findViewById(R.id.question1);
        buttonQuestion2 = (Button) findViewById(R.id.question2);
        buttonQuestion3 = (Button) findViewById(R.id.question3);
        buttonQuestion4 = (Button) findViewById(R.id.question4);
        buttonQuestion5 = (Button) findViewById(R.id.question5);
        buttonQuestion6 = (Button) findViewById(R.id.question6);
        buttonQuestion7 = (Button) findViewById(R.id.question7);
        buttonQuestion8 = (Button) findViewById(R.id.question8);
        buttonQuestion9 = (Button) findViewById(R.id.question9);
        buttonQuestion10 = (Button) findViewById(R.id.question10);
        buttonQuestion11 = (Button) findViewById(R.id.question11);
        buttonQuestion12 = (Button) findViewById(R.id.question12);
        buttonQuestion13 = (Button) findViewById(R.id.question13);
        buttonQuestion14 = (Button) findViewById(R.id.question14);
        buttonQuestion15 = (Button) findViewById(R.id.question15);

        homeModule.setOnClickListener(this);
        buttonQuestion1.setOnClickListener(this);
        buttonQuestion2.setOnClickListener(this);
        buttonQuestion3.setOnClickListener(this);
        buttonQuestion4.setOnClickListener(this);
        buttonQuestion5.setOnClickListener(this);
        buttonQuestion6.setOnClickListener(this);
        buttonQuestion7.setOnClickListener(this);
        buttonQuestion8.setOnClickListener(this);
        buttonQuestion9.setOnClickListener(this);
        buttonQuestion10.setOnClickListener(this);
        buttonQuestion11.setOnClickListener(this);
        buttonQuestion12.setOnClickListener(this);
        buttonQuestion13.setOnClickListener(this);
        buttonQuestion14.setOnClickListener(this);
        buttonQuestion15.setOnClickListener(this);

         //************** variable needed in onClick **************
        idListener1 = Long.parseLong(getIdListener.get(0));
        idListener2 = Long.parseLong(getIdListener.get(1));
        idListener3 = Long.parseLong(getIdListener.get(2));
        idListener4 = Long.parseLong(getIdListener.get(3));
        idListener5 = Long.parseLong(getIdListener.get(4));
        idListener6 = Long.parseLong(getIdListener.get(5));
        idListener7 = Long.parseLong(getIdListener.get(6));
        idListener8 = Long.parseLong(getIdListener.get(7));
        idListener9 = Long.parseLong(getIdListener.get(8));
        idListener10 = Long.parseLong(getIdListener.get(9));
        idListener11 = Long.parseLong(getIdListener.get(10));
        idListener12 = Long.parseLong(getIdListener.get(11));
        idListener13 = Long.parseLong(getIdListener.get(12));
        idListener14 = Long.parseLong(getIdListener.get(13));
        idListener15 = Long.parseLong(getIdListener.get(14));

        flagListener1 = Long.parseLong(getFlagListener.get(0));
        flagListener2 = Long.parseLong(getFlagListener.get(1));
        flagListener3 = Long.parseLong(getFlagListener.get(2));
        flagListener4 = Long.parseLong(getFlagListener.get(3));
        flagListener5 = Long.parseLong(getFlagListener.get(4));
        flagListener6 = Long.parseLong(getFlagListener.get(5));
        flagListener7 = Long.parseLong(getFlagListener.get(6));
        flagListener8 = Long.parseLong(getFlagListener.get(7));
        flagListener9 = Long.parseLong(getFlagListener.get(8));
        flagListener10 = Long.parseLong(getFlagListener.get(9));
        flagListener11 = Long.parseLong(getFlagListener.get(10));
        flagListener12 = Long.parseLong(getFlagListener.get(11));
        flagListener13 = Long.parseLong(getFlagListener.get(12));
        flagListener14 = Long.parseLong(getFlagListener.get(13));
        flagListener15 = Long.parseLong(getFlagListener.get(14));

        //************** variable needed in onCreate **************
        flag1 = getFlag.get(0);
        flag2 = getFlag.get(1);
        flag3 = getFlag.get(2);
        flag4 = getFlag.get(3);
        flag5 = getFlag.get(4);
        flag6 = getFlag.get(5);
        flag7 = getFlag.get(6);
        flag8 = getFlag.get(7);
        flag9 = getFlag.get(8);
        flag10 = getFlag.get(9);
        flag11 = getFlag.get(10);
        flag12 = getFlag.get(11);
        flag13 = getFlag.get(12);
        flag14 = getFlag.get(13);
        flag15 = getFlag.get(14);

        question1 = getQuestion.get(0);
        question2 = getQuestion.get(1);
        question3 = getQuestion.get(2);
        question4 = getQuestion.get(3);
        question5 = getQuestion.get(4);
        question6 = getQuestion.get(5);
        question7 = getQuestion.get(6);
        question8 = getQuestion.get(7);
        question9 = getQuestion.get(8);
        question10 = getQuestion.get(9);
        question11 = getQuestion.get(10);
        question12 = getQuestion.get(11);
        question13 = getQuestion.get(12);
        question14 = getQuestion.get(13);
        question15 = getQuestion.get(14);

        answer1 = getAnswer.get(0);
        answer2 = getAnswer.get(1);
        answer3 = getAnswer.get(2);
        answer4 = getAnswer.get(3);
        answer5 = getAnswer.get(4);
        answer6 = getAnswer.get(5);
        answer7 = getAnswer.get(6);
        answer8 = getAnswer.get(7);
        answer9 = getAnswer.get(8);
        answer10 = getAnswer.get(9);
        answer11 = getAnswer.get(10);
        answer12 = getAnswer.get(11);
        answer13 = getAnswer.get(12);
        answer14 = getAnswer.get(13);
        answer15 = getAnswer.get(14);
        //************** set button ************** 
        setButton();

        //************** set level header ************** 
        setLevelHeader();

        //************** view score and hint ************** 
        setScoreHint();
    }

    public void fetchData(){
        //************** fetching data ************** 
        mySQLiteAdapter = new SQLiteAdapter(getApplicationContext());
        mySQLiteAdapter.openToWrite();
        Cursor c = mySQLiteAdapter.getQuestionEachLevel(getLevelKey);
        if(c.moveToFirst()){
        do{
            getIdListener.add(c.getString(0));
            getAnswer.add(  c.getString(1));
            getQuestion.add(c.getString(2));
            getFlag.add(c.getString(4));
            getFlagListener.add(c.getString(4));
            }while(c.moveToNext());
        }
        mySQLiteAdapter.close();  
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
                case R.id.home_button:
                    startActivity(new Intent(QuestionList.this, Main.class));
                    break;
                case R.id.question1:
                    if(flagListener1 == 0){
                        Intent newIntent1 = new Intent(this, Answering.class);
                        newIntent1.putExtra(this.getPackageName(), idListener1);
                        this.startActivity(newIntent1);}
                break;
                case R.id.question2:
                    if(flagListener2 == 0){
                        Intent newIntent2 = new Intent(this, Answering.class);
                        newIntent2.putExtra(this.getPackageName(), idListener2);
                        this.startActivity(newIntent2);}
                break;
                case R.id.question3:
                    if(flagListener3 == 0){
                        Intent newIntent3 = new Intent(this, Answering.class);
                        newIntent3.putExtra(this.getPackageName(), idListener3);
                        this.startActivity(newIntent3);}
                break;
                case R.id.question4:
                    if(flagListener4 == 0){
                        Intent newIntent4 = new Intent(this, Answering.class);
                        newIntent4.putExtra(this.getPackageName(), idListener4);
                        this.startActivity(newIntent4);}
                break;
                case R.id.question5:
                    if(flagListener5 == 0){
                        Intent newIntent5 = new Intent(this, Answering.class);
                        newIntent5.putExtra(this.getPackageName(), idListener5);
                        this.startActivity(newIntent5);}
                break;
                case R.id.question6:
                    if(flagListener6 == 0){
                        Intent newIntent6 = new Intent(this, Answering.class);
                        newIntent6.putExtra(this.getPackageName(), idListener6);
                        this.startActivity(newIntent6);}
                break;
                case R.id.question7:
                    if(flagListener7 == 0){
                        Intent newIntent7 = new Intent(this, Answering.class);
                        newIntent7.putExtra(this.getPackageName(), idListener7);
                        this.startActivity(newIntent7);}
                break;
                case R.id.question8:
                    if(flagListener8 == 0){
                        Intent newIntent8 = new Intent(this, Answering.class);
                        newIntent8.putExtra(this.getPackageName(), idListener8);
                        this.startActivity(newIntent8);}
                break;
                case R.id.question9:
                    if(flagListener9 == 0){
                        Intent newIntent9 = new Intent(this, Answering.class);
                        newIntent9.putExtra(this.getPackageName(), idListener9);
                        this.startActivity(newIntent9);}
                break;
                case R.id.question10:
                    if(flagListener10 == 0){
                        Intent newIntent10 = new Intent(this, Answering.class);
                        newIntent10.putExtra(this.getPackageName(), idListener10);
                        this.startActivity(newIntent10);}
                break;
                case R.id.question11:
                    if(flagListener11 == 0){
                        Intent newIntent11 = new Intent(this, Answering.class);
                        newIntent11.putExtra(this.getPackageName(), idListener11);
                        this.startActivity(newIntent11);}
                break;
                case R.id.question12:
                    if(flagListener12 == 0){
                        Intent newIntent12 = new Intent(this, Answering.class);
                        newIntent12.putExtra(this.getPackageName(), idListener12);
                        this.startActivity(newIntent12);}
                break;
                case R.id.question13:
                    if(flagListener13 == 0){
                        Intent newIntent13 = new Intent(this, Answering.class);
                        newIntent13.putExtra(this.getPackageName(), idListener13);
                        this.startActivity(newIntent13);}
                break;
                case R.id.question14:
                    if(flagListener14 == 0){
                        Intent newIntent14 = new Intent(this, Answering.class);
                        newIntent14.putExtra(this.getPackageName(), idListener14);
                        this.startActivity(newIntent14);}
                break;
                case R.id.question15:
                    if(flagListener15 == 0){
                        Intent newIntent15 = new Intent(this, Answering.class);
                        newIntent15.putExtra(this.getPackageName(), idListener15);
                        this.startActivity(newIntent15);}
                break;

             }
        }
    public void setLevelHeader(){
        //************** set level header image **************
        if(getLevelKey == 1){
            header = (ImageView) findViewById(R.id.level_header);
            header.setImageResource(R.drawable.header_level1);
            category = (ImageView) findViewById(R.id.category_header);
            category.setImageResource(R.drawable.header_fruit);
        }else if(getLevelKey == 2){
            header = (ImageView) findViewById(R.id.level_header);
            header.setImageResource(R.drawable.header_level2);
            category = (ImageView) findViewById(R.id.category_header);
            category.setImageResource(R.drawable.header_animal);
        }else if(getLevelKey == 3){
            header = (ImageView) findViewById(R.id.level_header);
            header.setImageResource(R.drawable.header_level3);
            category = (ImageView) findViewById(R.id.category_header);
            category.setImageResource(R.drawable.header_music);
        }else if(getLevelKey == 4){
            header = (ImageView) findViewById(R.id.level_header);
            header.setImageResource(R.drawable.header_level4);
            category = (ImageView) findViewById(R.id.category_header);
            category.setImageResource(R.drawable.header_city);
        }else if(getLevelKey == 5){
            header = (ImageView) findViewById(R.id.level_header);
            header.setImageResource(R.drawable.header_level5);
            category = (ImageView) findViewById(R.id.category_header);
            category.setImageResource(R.drawable.header_sport);
        }else if(getLevelKey == 6){
            header = (ImageView) findViewById(R.id.level_header);
            header.setImageResource(R.drawable.header_level6);
            category = (ImageView) findViewById(R.id.category_header);
            category.setImageResource(R.drawable.header_food);
        }else if(getLevelKey == 7){
            header = (ImageView) findViewById(R.id.level_header);
            header.setImageResource(R.drawable.header_level7);
            category = (ImageView) findViewById(R.id.category_header);
            category.setImageResource(R.drawable.header_verb);
        }else if(getLevelKey == 8){
            header = (ImageView) findViewById(R.id.level_header);
            header.setImageResource(R.drawable.header_level8);
            category = (ImageView) findViewById(R.id.category_header);
            category.setImageResource(R.drawable.header_province);
        }else{
            header = (ImageView) findViewById(R.id.level_header);
            header.setImageResource(R.drawable.header_level9);
            category = (ImageView) findViewById(R.id.category_header);
            category.setImageResource(R.drawable.header_figure);
        }

    }

    public void setScoreHint(){
        //**************  get score from AppPreferences ************** 
        AppPreferences AppPref = new AppPreferences(this);
        int score = AppPref.getScore();
        int hint = AppPref.getHintLeft();

        //************** view score and hint in XML ************** 
        TextView t1 = (TextView)findViewById(R.id.textValueHint);
        TextView t2 = (TextView)findViewById(R.id.textValueScore);
        t1.setTextSize(18);
        t2.setTextSize(18);
        t1.setText(Long.toString(hint));
        t2.setText(Integer.toString(score));

    }
    public void setButton(){

        //************** set question button **************
        if ( flag1.equals("0")) {
            buttonQuestion1.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion1.setText(question1);
        } else {
            buttonQuestion1.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion1.setText(answer1);
            }   

        if ( flag2.equals("0")) {
            buttonQuestion2.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion2.setText(question2);
        } else {
            buttonQuestion2.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion2.setText(answer2);}

        if ( flag3.equals("0")) {
            buttonQuestion3.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion3.setText(question3);
        } else {
            buttonQuestion3.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion3.setText(answer3);}

        if ( flag4.equals("0")) {
            buttonQuestion4.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion4.setText(question4);
        } else {
            buttonQuestion4.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion4.setText(answer4);}

        if ( flag5.equals("0")) {
            buttonQuestion5.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion5.setText(question5);
        } else {
            buttonQuestion5.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion5.setText(answer5);}

        if ( flag6.equals("0")) {
            buttonQuestion6.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion6.setText(question6);
        } else {
            buttonQuestion6.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion6.setText(answer6);}

        if ( flag7.equals("0")) {
            buttonQuestion7.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion7.setText(question7);
        } else {
            buttonQuestion7.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion7.setText(answer7);}

        if ( flag8.equals("0")) {
            buttonQuestion8.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion8.setText(question8);            
        } else {
            buttonQuestion8.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion8.setText(answer8);}

        if ( flag9.equals("0")) {
            buttonQuestion9.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion9.setText(question9);
        } else {
            buttonQuestion9.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion9.setText(answer9);}

        if ( flag10.equals("0")) {
            buttonQuestion10.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion10.setText(question10);
        } else {
            buttonQuestion10.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion10.setText(answer10);}

        if ( flag11.equals("0")) {
            buttonQuestion11.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion11.setText(question11);
        } else {
            buttonQuestion11.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion11.setText(answer11);}

        if ( flag12.equals("0")) {
            buttonQuestion12.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion12.setText(question12);
        } else {
            buttonQuestion12.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion12.setText(answer12);}

        if ( flag13.equals("0")) {
            buttonQuestion13.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion13.setText(question13);
        } else {
            buttonQuestion13.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion13.setText(answer13);}

        if ( flag14.equals("0")) {
            buttonQuestion14.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion14.setText(question14);
        } else {
            buttonQuestion14.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion14.setText(answer14);}

        if ( flag15.equals("0")) {
            buttonQuestion15.setBackgroundResource(R.drawable.yellow_button);
            buttonQuestion15.setText(question15);
        } else {
            buttonQuestion15.setBackgroundResource(R.drawable.buttonchecklist);
            buttonQuestion15.setText(answer15);}
    }
}
}
4

4 回答 4

2

您应该清除活动暂停事件的值。

public void onPause() {
    super.onPause();

    getAnswer.clear();
    getQuestion.clear()   
}
于 2013-05-31T05:42:04.930 回答
1

您需要使用.clear()方法ListView来删除listview数据,在方法中调用此onDestory()方法

IE

@Override
public void onDestroy() {
    super.onDestroy();

    getAnswer.clear();
    getQuestion.clear() 
    // code of other listview which you want to clear
}
于 2013-05-31T05:08:03.357 回答
1

onCreate在方法关闭后放置以下代码:

@Override
public void onDestroy() {
    super.onDestroy();

    getAnswer.clear();
    getQuestion.clear()   
}
于 2013-05-31T05:43:59.393 回答
0

您需要调用getAnswer.clear();以清除该列表。同样,您可以在调用finish()活动时清除所有列表

于 2013-05-31T05:02:26.707 回答