gpt4 book ai didi

java - 可运行不起作用

转载 作者:行者123 更新时间:2023-12-01 17:01:54 26 4
gpt4 key购买 nike

我的 Runnable 有问题。它不想工作,我不知道问题是什么,因为它在我的其他应用程序中工作......有人可以帮助我吗?举例来说,有默认值=5的“healt”,对于我做了一个按钮的测试,当我单击按钮时,healt值-1...并且我想在health<5时启动Runnable...并添加+1为了健康......为了测试我设置了 Runnable 1 秒

这是代码:

MainActivity.java

package com.mygame.test;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;


import static com.mygame.test.variables.coin;
import static com.mygame.test.variables.health;


public class Main2Activity extends AppCompatActivity {

public static final String PREFS_NAME_MAIN = "mainpref";

TextView coinText, healthText, textView;
Button button3;

private final Context app = this;
private final Handler update = new Handler();
private final Runnable updateHealth = new Runnable()
{
public void run()
{
if (variables.run)
{
healthText.setText(""+health);


update.postDelayed(updateHealth, 500);
}
else if (!variables.run) update.removeCallbacksAndMessages(null);
}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);

coinText = findViewById(R.id.coinText);

healthText = findViewById(R.id.healthText);

healthText.setText(String.valueOf(health));

button3 = findViewById(R.id.button3);

textView = findViewById(R.id.textView);




Button closeButton = findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//finishAffinity();
startActivity(new Intent(Main2Activity.this, ExitActivity.class));
}
});

Button coinsplusButton = findViewById(R.id.coinsplusButton);
coinsplusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//finishAffinity();
startActivity(new Intent(Main2Activity.this, StoreActivity.class));
}
});


Button level1Button = findViewById(R.id.level1Button);
level1Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//finishAffinity();
startActivity(new Intent(Main2Activity.this, Level1Activity.class));
}
});


}

public void buttontest(View button3)
{
if (health > 0) {
health = health-1;
healthText.setText(""+health);

}
variables.run = true;
Intent activeClick = new Intent(this, ClickService.class);
this.startService(activeClick);
update.post(updateHealth);
save();


}


@Override
protected void onStart()
{
super.onStart();
load();
textupgrade();
if (!variables.run)
{
variables.run = true;
Intent activeClick = new Intent(this, ClickService.class);
this.startService(activeClick);
}
}

protected void onResume()
{
super.onResume();
load();
textupgrade();
update.post(updateHealth);

}

private void load()
{
SharedPreferences varReader = app.getSharedPreferences(PREFS_NAME_MAIN, MODE_PRIVATE);
variables.run = varReader.getBoolean("run", false);
coin = varReader.getInt("coin", 0);
health = varReader.getInt("health", 5);

}

private void save()
{
SharedPreferences.Editor varReader = app.getSharedPreferences(PREFS_NAME_MAIN, 0).edit();
varReader.putBoolean("run", variables.run);
varReader.putInt("coin", coin);
varReader.putInt("health", health);
varReader.apply();
}

private void textupgrade(){
coinText.setText(""+coin);
healthText.setText(""+health);

}


}

ClickService.java

package com.mygame.test;

import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;

public class ClickService extends IntentService
{
private final Handler handler = new Handler();
private final Runnable addHealth = new Runnable()
{
public void run()
{
variables.health++;
if (!variables.run) handler.removeCallbacksAndMessages(null);
else if (variables.run) handler.postDelayed(addHealth, 1000);
}
};
public ClickService()
{
super("ClickService");
}
@Override
protected void onHandleIntent(Intent activeClick)
{
handler.postDelayed(addHealth, 500);
}
}

变量.java

package com.mygame.test;

public class variables {
static int coin;
static int health;
static boolean run;


}

最佳答案

哦,我修复了它:D ClickService 未在 AndroidManifest 中注册

<service
android:name=".ClickService"
android:exported="false" />

关于java - 可运行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61501122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com