有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java静态变量在从一个活动移动到另一个活动时重新初始化自身

我使用的是一个公共类名常量,其中包含静态变量

/*
 * Flags
 */
public static boolean gotCourse = false;
public static boolean quizTaken = false;

我在第一个活动中更改这些标志,并移动到第二个(我也退出第一个活动),但在第二个活动中,我得到的是标志的初始值,而不是更改后的值。为什么我会遇到这个问题??应用程序的静态变量是否有限制

我也不想通过intent传递这些值,因为它们会使我的逻辑复杂化

第一项活动的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moviewrapper);
        Constants.quizTaken = extra.getBoolean("fromquiz", false);
        Log.d(Constants.TAG,"Got Course Value: "+Constants.gotCourse);
        if(Constants.gotCourse == false){
            mGetDataTask = new GetDataTask();
            mGetDataTask.execute();
            Constants.gotCourse = true;
        }else{
            Log.d(Constants.TAG,"In Play");
            playProgram();
        }
        Log.d(Constants.TAG,"Got Course Value: "+Constants.gotCourse);

    }

其中我正在更改Constants.gotCourse = true;

第二项活动代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(Constants.TAG,"Got Course Value in Video Wrapper: "+Constants.gotCourse);
    }

任务代码:

private class GetDataTask extends AsyncTask<String, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(MovieWrapperActivity.this);

    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.setCancelable(false);
        this.dialog.show();
    }

    // automatically done on worker thread (separate from UI thread)
    protected Void doInBackground(final String... args) {
        getCourse();
        return null;
    }

    // can use UI thread here
    protected void onPostExecute(final Void unused) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
            playProgram();
        }
    }
}

可编程播放功能:

private void playProgram(){
        Intent intent = new Intent(MovieWrapperActivity.this, VideoWrapperActivity.class);

        int chap_played = extra.getInt("chapterplayed",-1);
        Log.d(Constants.TAG,"Played: "+chap_played +" : chapter: "+Constants.mCourse.getTotalChapters());
        if(chap_played == -1){
            //Play 1st Chapter
            intent.putExtra("chaptertoplay", 1);
            //Chapter cha = mCourse.getmChapters().get(1);
            intent.putExtra("videofile",  Constants.mCourse.getmChapters().get(0).getVideoURL());
            if(Constants.mCourse.getmChapters().get(0).isQuiz()){
                Constants.quizTaken = false;
            }else{
                Constants.quizTaken = true;
            }
        }
        else if (chap_played < Constants.mCourse.getmChapters().size()){
            // For Playing 2nd to till last
            if(Constants.mCourse.getmChapters().get(chap_played-1).isQuiz() && !Constants.quizTaken){

                Intent in2 = new Intent(MovieWrapperActivity.this, QuestionWrapper.class);
                in2.putExtra("chaptertoplay", chap_played);
                Constants.quizTaken = true;
                startActivity(in2);
                //System.exit(0);

            }else{
                if(Constants.mCourse.getmChapters().get(chap_played).isQuiz()){
                    Constants.quizTaken = false;
                }else{
                    Constants.quizTaken = true;
                }
                intent.putExtra("videofile",  Constants.mCourse.getmChapters().get(chap_played).getVideoURL());
                intent.putExtra("chaptertoplay", chap_played+1);
            }

        }
        else if(chap_played == Constants.mCourse.getmChapters().size()){
            // Move to End Knowlege Test Here
            MovieWrapperActivity.this.finish();
        }
        Log.d(Constants.TAG,"Here...");
        startActivity(intent);
        System.exit(0);

    }

编辑:还有一件事。当我没有完成第一个活动并开始第二个活动时,我在第二个活动中得到了改变的值

更新:当我用System.exit(0);完成活动时会出现问题,但在finish();完成时不会出现问题。但是当使用finish();完成活动时,会导致更多的运行时错误


共 (2) 个答案

  1. # 1 楼答案

    您应该使用getter和setter将这些值提取到共享资源类中。这样,他们的内容就可以在任何地方检索到

  2. # 2 楼答案

    这是因为有两条不同的线索。现在,在本例中,第二个活动在gotCourse变量更改为true之前启动。所以,如果你做下面这样的事情,它会解决你的问题

     if(Constants.gotCourse == false){
             Constants.gotCourse = true;
            mGetDataTask = new GetDataTask();
            mGetDataTask.execute();
    
        }else{