怎么从MainActivity中获取值?

huangapple go评论73阅读模式
英文:

How to get value from MainActivity?

问题

所以我有这个主活动:

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
private volatile boolean stopTask = false;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layout);
   }
   
   public boolean getStopTask() {
      return stopTask;
   }
   
   public void startMyTask(View view) {
      stopTask = false;
      TextView textView = findViewById(R.id.text_view);
      MyTask myTask = new MyTask(10, textView);

      new Thread(myTask).start();
   }
   
   public void stopMyTask(View view) {
      stopTask = true;
   }
}

有人告诉我可以通过获取MainActivity的实例并直接访问该函数来从MyTask中访问getStopTask()

我尝试了,但是我收到了一个错误:"Can't create handler inside thread Thread[Thread-6,5,main] that has not called Looper.prepare()"

这是MyTask

import android.widget.TextView;

public class MyTask implements Runnable {
    int seconds;
    TextView textView;

    MyTask(int seconds, TextView textView) {
        this.seconds = seconds;
        this.textView = textView;
    }

    @Override
    public void run() {

    MainActivity mainActivity = new MainActivity();

    for (int x = 0; x < seconds; x++) {
        boolean stopTask = mainActivity.getStopTask();

        if (stopTask)
           break;

        textView.setText(String.format("x: %s", x));

        try {
           Thread.sleep(1000);
        } catch (InterruptedException e) {
           e.printStackTrace();
        }    
      }
    }
}

错误发生在应用程序触及以下部分之后:

MainActivity mainActivity = new MainActivity();

我怎样才能从MyTask获取stopTask的值?

英文:

So I have this main activity:

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
private volatile boolean stopTask = false;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layout);
   }
   
   public boolean getStopTask() {
      return stopTask;
   }
   
   public void startMyTask(View view) {
      stopTask = false;
      TextView textView = findViewById(R.id.text_view);
      MyTask myTask = new MyTask(10, textView);

      new Thread(myTask).start();
   }
   
   public void stopMyTask(View view) {
      stopTask = true;
   }
}

I was told that I could access getStopTask() from MyTask by getting an instance of MainActivity and accessing that function directly.

I tried that, but I get an error: "Can't create handler inside thread Thread[Thread-6,5,main] that has not called Looper.prepare()".

Here's MyTask:

import android.widget.TextView;

public class MyTask implements Runnable {
    int seconds;
    TextView textView;

    MyTask(int seconds, TextView textView) {
        this.seconds = seconds;
        this.textView = textView;
    }

    @Override
    public void run() {

    MainActivity mainActivity = new MainActivity();

    for (int x = 0; x &lt; seconds; x++) {
        boolean stopTask = mainActivity.getStopTask();

        if (stopTask)
           break;

        textView.setText(String.format(&quot;x: %s&quot;, x));

        try {
           Thread.sleep(1000);
        } catch (InterruptedException e) {
           e.printStackTrace();
        }    
      }
    }
}

The error happens right after the app hits:

MainActivity mainActivity = new MainActivity();

How can I get the value of stopTask from MyTask?

答案1

得分: 0

我会将布尔变量stopTask改为静态,并在代码中使用它,取代getStopTask()的方式:

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    public static boolean stopTask = false;

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

    public void startMyTask(View view) {
        stopTask = false;
        TextView textView = findViewById(R.id.text_view);
        MyTask myTask = new MyTask(10, textView);

        new Thread(myTask).start();
    }

    public void stopMyTask(View view) {
        stopTask = true;
    }
}

MyTask.java:

public class MyTask implements Runnable {
    int seconds;
    TextView textView;

    MyTask(int seconds, TextView textView) {
        this.seconds = seconds;
        this.textView = textView;
    }

    @Override
    public void run() {
        for (int x = 0; x < seconds; x++) {
            boolean stopTask = MainActivity.stopTask;

            if (stopTask)
               break;

            textView.setText(String.format("x: %s", x));

            try {
               Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }    
        }
    }
}
英文:

I would turn boolean stopTask to static and use it instead of getStopTask() this way:

MainActivity.java:

public class MainActivity extends AppCompatActivity {
public static boolean stopTask = false;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layout);
   }
   
   /*
   
   public boolean getStopTask() {
      return stopTask;
   }*/
   
   public void startMyTask(View view) {
      stopTask = false;
      TextView textView = findViewById(R.id.text_view);
      MyTask myTask = new MyTask(10, textView);

      new Thread(myTask).start();
   }
   
   public void stopMyTask(View view) {
      stopTask = true;
   }
}

MyTask.java:

public class MyTask implements Runnable {
    int seconds;
    TextView textView;

    MyTask(int seconds, TextView textView) {
        this.seconds = seconds;
        this.textView = textView;
    }

    @Override
    public void run() {

    //MainActivity mainActivity = new MainActivity();

    for (int x = 0; x &lt; seconds; x++) {
        boolean stopTask = MainActivity.stopTask;

        if (stopTask)
           break;

        textView.setText(String.format(&quot;x: %s&quot;, x));

        try {
           Thread.sleep(1000);
        } catch (InterruptedException e) {
           e.printStackTrace();
        }    
      }
    }
}

huangapple
  • 本文由 发表于 2020年9月25日 04:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64053795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定