Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 静态变量在从一个活动移动到另一个活动时重新初始化自身_Java_Android_Static Variables - Fatal编程技术网

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

Java 静态变量在从一个活动移动到另一个活动时重新初始化自身,java,android,static-variables,Java,Android,Static Variables,我使用的是一个公共类名常量,其中包含静态变量 /* * Flags */ public static boolean gotCourse = false; public static boolean quizTaken = false; 我在第一个活动中更改这些标志,并移动到第二个(我也退出第一个活动),但在第二个活动中,我得到的是标志的初始值,而不是更改后的值。为什么我会遇到这个问题??应用程序的静态变量是否有限制 我也不想通过intent传递这些值,因为它们会使我的逻辑复杂化 第一项活

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

/*
 * 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);

    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(Constants.TAG,"Got Course Value in Video Wrapper: "+Constants.gotCourse);
    }
其中我正在更改
Constants.gotCourse=true

第二项活动代码:

@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);

    }
@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();
        }
    }
}
私有类GetDataTask扩展了AsyncTask{
private final ProgressDialog dialog=新建ProgressDialog(MovieWrapperActivity.this);
//可以在这里使用UI线程
受保护的void onPreExecute(){
this.dialog.setMessage(“加载…”);
this.dialog.setCancelable(false);
this.dialog.show();
}
//在工作线程上自动完成(与UI线程分开)
受保护的Void doInBackground(最终字符串…args){
getCourse();
返回null;
}
//可以在这里使用UI线程
后期执行时受保护的无效(最终无效未使用){
if(this.dialog.isShowing()){
this.dialog.disclose();
播放程序();
}
}
}
可编程播放功能:

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);

    }
private void playProgram(){
Intent Intent=newintent(MovieWrapperActivity.this,VideoWrapperActivity.class);
int chap_played=extra.getInt(“chapterplayed”,-1);
Log.d(Constants.TAG,“Played:+chap_Played+”:chapter:“+Constants.mCourse.getTotalChapters());
如果(播放的章==-1){
//第1章
意图。putExtra(“chaptertoplay”,1);
//Chapter cha=mCourse.getmChapters().get(1);
intent.putExtra(“videofile”,Constants.mCourse.getmChapters().get(0.getVideoURL());
if(Constants.mCourse.getmChapters().get(0.isquick()){
Constants.quizTaken=false;
}否则{
Constants.quizTaken=true;
}
}
else if(chap_播放

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


更新:当我使用
系统完成活动时出现问题。退出(0)但通过
finish()完成时没有问题。但当使用
finish()完成活动时它会导致更多的运行时错误

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

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

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

    }else{

没有足够的代码来回答您的问题,可能错误在其他地方。这里似乎没有错误,请给出完整的代码,一旦更改了静态变量的值,所有对象和变量都将获得更改的值,我不会在其他任何地方更改值…当我没有完成第一个活动并开始第二个活动时,我会在第二个活动中获得更改的值。我在完成第一个活动时遇到的问题。execute()方法在做什么?我从未使用过共享资源类类型的东西。请你提供一些例子。我在谷歌上找不到。你在哪里完成活动?
playProgram
我的活动的函数,该函数在我的
AsyncTask
code of
playProgram
中被调用。我真的不相信它不起作用。。。。在执行GetDataTask()之前是否更改了变量?如果是,请在执行任务之前尝试放置一个线程。sleep(),看看它是否有效。@AhmedNawaz您是否设法解决了此问题…了解解决方案会很有趣吗?您解决了此问题。现在,我没有通过调用
startActivityForResult
来完成基本活动并打开子活动。实际上,系统。退出(0)是此问题的原因。。