您是否使用与Android Studio中的方法不同的方法?我有以下主要课程:
package com.example.bluebus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.SECONDS;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BeeperControl test = new BeeperControl();
//test.test();
//test.test();*/
}
}
在同一文件夹中我有以下BeeperControl类:
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* Created by Arthur on 1/7/2017.
*/
public class BeeperControl extends AppCompatActivity {
/*private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);*/
final TextView text = (TextView) findViewById(R.id.text);
/*
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() {
text.setText("Hello!");
}
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 0, 1, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}*/
public void test(){
text.setText("Who are you?");
}
}
按原样编写时,该应用程序正常工作。但是,当我取消注释test.test()时,当我尝试在模拟器中运行它时,应用程序会保持关闭状态。从我所看到的情况来看,更新清单可能存在问题,但我不确定如何修复它。任何帮助将不胜感激!
答案 0 :(得分:0)
让类中的方法返回值,findbyid将返回null,因为它超出范围。
例如:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.SECONDS;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BeeperControl test = new BeeperControl();
TextView text = (TextView) findViewById(R.id.text);
text.setText(test.test());
//test.test();*/
}
}
你的班级:
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* Created by Arthur on 1/7/2017.
*/
public class BeeperControl extends AppCompatActivity {
/*private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);*/
/* final TextView text = (TextView) findViewById(R.id.text);^/
/*
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() {
text.setText("Hello!");
}
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 0, 1, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}*/
public string test(){
return ("Who are you?");
}
}
如果您通过引用传递文本(可能会为您执行此操作),这可能对您在任何使用的上下文中使用该类都没有帮助。 R / 普雷斯科特......