我正在尝试通过带有OnClickListener的按钮从MainActivity调用一个活动
第一个活动是响应并且有效,第二个活动不起作用并且崩溃了应用程序
请帮我找到解决方案
感谢!!!
这是代码:
class Layout{
public Layout(){
btnStopWatch = (Button)findViewById(R.id.btnStopWatch);
btnTimer = (Button)findViewById(R.id.btnTimer);
}
Button btnStopWatch, btnTimer;
}
class Event{
public Event(){
l.btnStopWatch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), StopWatchActivity.class);
startActivity(i);
}
});
l.btnTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), TimerActivity.class);
startActivity(i);
}
});
}
}
Layout l; Event e;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = new Layout(); e = new Event();
}}
第一个有效的活动:
公共类StopWatchActivity扩展了AppCompatActivity {
class Layout{
public Layout(){
tvHours = (TextView)findViewById(R.id.tvHours);
tvMinutes = (TextView)findViewById(R.id.tvMinutes);
tvSeconds = (TextView)findViewById(R.id.tvSeconds);
tvBack = (TextView)findViewById(R.id.tvBack);
btnStartStop = (Button)findViewById(R.id.btnStartStop);
btnReset = (Button)findViewById(R.id.btnReset);
}
TextView tvHours, tvMinutes, tvSeconds, tvBack;
Button btnStartStop, btnReset;
}
class Event{
public Event(){
l.btnStartStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isStarted){
Stop();
}else {
Start();
}
}
});
l.btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Reset();
}
});
l.tvBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
}
}
Layout l; Event e;
boolean isStarted = false;
int sec = 0, min = 0, hour = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_watch);
l = new Layout(); e = new Event();
}
public void Start(){
l.btnStartStop.setText("Stop");
isStarted = true;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (isStarted){
try{
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
if(isStarted){
MoveTime();
}
}
});
}catch (InterruptedException e){
}
}
}
});
t.start();
}
public void MoveTime(){
sec++;
if (sec >= 59){
sec = 0;
min++;
if(min >= 59){
min = 0;
hour++;
}
}
UpdateScreen();
}
public void UpdateScreen(){
l.tvHours.setText(Format(hour));
l.tvMinutes.setText(Format(min));
l.tvSeconds.setText(Format(sec));
}
public void CleanScreen(){
l.tvHours.setText("00");
l.tvMinutes.setText("00");
l.tvSeconds.setText("00");
}
public String Format(int i){
if (i<=9){
return "0" + i;
}else{
return Integer.toString(i);
}
}
public void Stop(){
isStarted = false;
l.btnStartStop.setText("Start");
}
public void Reset(){
if(isStarted)
Stop();
CleanScreen();
sec = 0;
min = 0;
hour = 0;
}}
不起作用的第二项活动:
公共类TimerActivity扩展了AppCompatActivity {
class Layout {
public Layout() {
etHour = (EditText) findViewById(R.id.etHour);
etHour.setFilters(new InputFilter[]{new InputFilterMinMax("0", "24")});
etMin = (EditText) findViewById(R.id.etMin);
etMin.setFilters(new InputFilter[]{new InputFilterMinMax("0", "59")});
etSec = (EditText) findViewById(R.id.etSec);
etSec.setFilters(new InputFilter[]{new InputFilterMinMax("0", "59")});
btnStartStop = (Button) findViewById(R.id.btnStartStop);
btnReset = (Button) findViewById(R.id.btnReset);
tvBack1 = (TextView)findViewById(R.id.tvBack);
}
EditText etHour, etMin, etSec;
Button btnStartStop, btnReset;
TextView tvBack1;
}
class Event {
public Event() {
l.btnStartStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isStarted) {
Stop();
} else {
Start();
}
}
});
l.btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Reset();
}
});
l.tvBack1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
}
}
Layout l;
Event e;
boolean isStarted = false;
int sec = 0, min = 0, hour = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
l = new Layout();
e = new Event();
}
public void Start() {
l.btnStartStop.setText("Stop");
isStarted = true;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (isStarted) {
try {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (isStarted) {
MoveTime();
}
}
});
} catch (InterruptedException e) {
}
}
}
});
t.start();
}
public void MoveTime() {
sec = Integer.valueOf(l.etSec.getText().toString());
min = Integer.valueOf(l.etMin.getText().toString());
hour = Integer.valueOf(l.etHour.getText().toString());
sec--;
if (sec < 0) {
sec = 59;
min--;
if (min < 0) {
min = 59;
hour--;
}
}
UpdateScreen();
if (hour == 0) {
if (min == 0) {
if (sec == 0) {
Stop();
}
}
}
}
public void UpdateScreen(){
l.etHour.setText(Format(hour));
l.etMin.setText(Format(min));
l.etSec.setText(Format(sec));
}
public void CleanScreen(){
l.etHour.setText("00");
l.etMin.setText("00");
l.etSec.setText("00");
}
public String Format(int i){
if (i<=9){
return "0" + i;
}else{
return Integer.toString(i);
}
}
public void Stop(){
isStarted = false;
l.btnStartStop.setText("Start");
}
public void Reset(){
if(isStarted)
Stop();
CleanScreen();
sec = 0;
min = 0;
hour = 0;
}}