简体   繁体   English

如何从 MainActivity 获取价值?

[英]How to get value from MainActivity?

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.有人告诉我,我可以通过获取MainActivity的实例并直接访问该函数来从MyTask访问getStopTask()

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()" .我试过了,但出现错误: “无法在尚未调用 Looper.prepare() 的线程 Thread[Thread-6,5,main] 内创建处理程序”

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

The error happens right after the app hits:应用程序命中后立即发生错误:

MainActivity mainActivity = new MainActivity();

How can I get the value of stopTask from MyTask?如何从 MyTask 获取stopTask的值?

I would turn boolean stopTask to static and use it instead of getStopTask() this way:我会将 boolean stopTask为 static 并以这种方式使用它而不是getStopTask()

MainActivity.java:主活动.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:我的任务.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 < 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();
        }    
      }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从RecycleAdapter获取价值并设置为MainActivity Text? - How to get value from RecycleAdapter and set to MainActivity Text? 如何从 Mainactivity 获取一个字符串值到 cardview 适配器? - How to get one string value from the Mainactivity to a cardview Adapter? 如何将MainActivity中的值传递给ServiceConnection? - How pass a value from MainActivity to ServiceConnection? Android中如何从DialogFragment获取数据到MainActivity? - How to get the Data from DialogFragment to MainActivity in Android? 如何从 AsyncTask 中的服务器获取字符串到 MainActivity? - How to get string from a server in AsyncTask to a MainActivity? 如何从 SQLite 表列中获取最大值并将其返回到 MainActivity 中的变量。 安卓工作室 - How to get max value from SQLite table column and return it to variable in MainActivity. Android Studio 如何将方法从MainActivity运行到我的值类? - How can I run method from MainActivity to my value class? 如何从非活动类获取 MainActivity Arraylist? - How to get MainActivity Arraylist from non Activity class? Android:如何从Fragment中的MainActivity获取MediaPlayer对象? - Android: how to get a MediaPlayer object from the MainActivity in a Fragment? 如何从MainActivity获取对ListFragment的适配器和基础数据的引用? - How to get a reference to ListFragment's adapter and underlying data from MainActivity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM