【问题标题】:static variables reinitializing themselves when moving from one activity to other静态变量在从一个活动移动到另一个活动时重新初始化
【发布时间】:2013-04-10 03:15:17
【问题描述】:

我正在使用一个公共类名常量,其中有静态变量。

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

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

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

第一个活动的代码:

@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(); 完成活动时,它会导致更多的运行时错误。

【问题讨论】:

  • 没有足够的代码来回答你的问题,可能错误在别处。
  • 这里似乎没有错误,给出完整的代码,一旦你改变了静态变量的值,那么所有的对象和变量都会得到改变的值
  • 我不会在其他任何地方改变价值......
  • 当我没有完成第一个活动并开始第二个活动时,我在第二个活动中得到了改变。完成第一项活动时遇到的问题。
  • execute() 方法是做什么的?

标签: java android static-variables


【解决方案1】:

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

【讨论】:

  • 我从来没有使用过共享资源类的东西。请你提供一些例子。我在谷歌上找不到。
【解决方案2】:

这是因为有两个不同的线程。现在在这种情况下,第二个活动在 gotCourse 变量更改为 true 之前启动。因此,如果您执行以下操作,它将解决您的问题。

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

    }else{

【讨论】:

  • 你在哪里完成活动?
  • playProgram 我的活动的函数,该函数在我的 AsyncTask 代码中的 onPostExecute 中调用,playProgram 添加在问题中。
  • 我不太相信它不起作用....您在执行 GetDataTask() 之前更改了变量吗?如果是,请您在执行任务之前尝试放置一个 Thread.sleep() 看看它是否有效..
  • @AhmedNawaz 您是否设法解决了这个问题...知道解决方案会很有趣吗?
  • Ues 解决了这个问题。现在我没有通过调用 startActivityForResult 完成基本活动并打开子活动。实际上 System.exit(0) 是这个问题的原因..
猜你喜欢
  • 1970-01-01
  • 2015-02-16
  • 2013-07-05
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
相关资源
最近更新 更多