简体   繁体   English

Java:如何在另一个包中执行TextView.setText?

[英]Java: How to do TextView.setText in another package?

I'm new to Java, I'm trying to build something in Android Studio. 我是Java新手,正在尝试在Android Studio中构建一些东西。 The point is to 'push' a value for TextView baseTickVar in class BaseScreen, from another PACKAGE, where the class CoreFunctionality resides. 关键是要从另一个类CoreFunctionality所在的PACKAGE中“推送” BaseScreen类中TextView baseTickVar的值。 Each time the tickNumber increases I want that shown in the TextView, so it's not a one time setting of text. 每次tickNumber增加时,我都希望在TextView中显示它,因此这不是一次性设置文本。

I have tried interfaces, but interfacing won't allow variables, only constants. 我已经尝试过接口,但是接口将不允许变量,仅允许常量。 I've tried TextView.setText from the CoreFunctionality package, but that gave a nullpointerException and declaring the TextView to counter that didn't seem to help. 我已经尝试过CoreFunctionality包中的TextView.setText,但是它给出了nullpointerException并声明了TextView来对抗似乎没有帮助。

public class BaseScreen extends Activity implements View.OnClickListener {

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

    // some irrelevant code here so i left it out.

    TextView baseTickVar = (TextView)findViewById(R.id.baseTickVar);        
    baseTickVar.setText("1"); // just to not have it empty...

}

Now I want to set value of baseTickVar with a variable from the other package CoreFunctionality 现在,我想使用另一个包CoreFunctionality中的变量设置baseTickVar的值

public class CoreFunctionality extends Activity implements Runnable {

Thread tickThread = null;
volatile boolean playingGalactic;
long lastTick;
public int tickNumber;
int tickLength;
TextView baseTickVar;

public void controlTicks() {

    tickLength = 2000;
    long timeThisTick = (System.currentTimeMillis() - lastTick); 
    long timeToWait = tickLength - timeThisTick; 
    if (timeToWait > 0) {
        try {
            tickThread.sleep(timeToWait);
        } catch (InterruptedException e) {
        }
    }
    lastTick = System.currentTimeMillis();
}

@Override
public void run() {

    while (playingGalactic) {
        controlTicks();
        tickNumber++;
        Log.i("Tick number ", "" + tickNumber);
        updateTick();
    }
}

private void updateTick() {
    // this is the whole point...
    baseTickVar.setText("" + tickNumber);
}

public void resume() {
    playingGalactic = true;
    tickThread = new Thread(this);
    tickThread.start(); 

} }

I guess your BaseScreen is the main screen and CoreFunctionality is some component that is doing some work. 我猜您的BaseScreen是主屏幕,而CoreFunctionality是正在执行某些工作的某些组件。 Actually CoreFunctionality does not need to be Activity , it better fits to be a service. 实际上, CoreFunctionality不必是Activity ,它更适合作为服务。

  1. You have to somehow pass reference to baseTickVar to the CoreFunctionality . 您必须以某种方式baseTickVar引用baseTickVarCoreFunctionality
  2. It is not allowed to mess with UI elements (such as TextView ) from within another thread. 不允许从另一个线程中弄乱UI元素(例如TextView )。 You should consider using some inter-thread communication (such as Message ). 您应该考虑使用一些线程间通信(例如Message )。

Make BaseScreen to extend Handler or make a Handler object in it then override 使BaseScreen扩展Handler或在其中创建Handler对象,然后覆盖

public void handleMessage(Message msg) {
    baseTickVar.setText("" + msg.obj);
}

In CoreFunctionality CoreFunctionality

private void updateTick() {
    Message msg=new Message();
    msg.obj=tickNumber;
    h.sendMessage(msg);
}

Of course you'll have to pass the h reference to CoreFunctionality . 当然,您必须将h引用传递给CoreFunctionality

Maybe not 100% accurate but it should work with little tweaking. 也许不是100%准确,但应该稍作调整即可。 Hope this will help. 希望这会有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM