我做了一个 android 应用程序,我想从菜单中更改正在运行的线程的一些参数。我的班级看起来像这样:
public class MyClass {
public class MyThread extends Thread {
public volatile long sleepTime =200;
.....
public synchronized void set_sleepTime(long value)
/* with or without synchronized is te same problem*/
{
sleepTime=value;
}
.....
}
}
我在应用程序的 onCreateMethod 中创建类
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyClass nClass=new MyClass(this);
setContentView(nClass);
}
我尝试更改菜单中的参数。像这样:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.load_game:
...
try{
mSheepThread.set_sleepTime(10);
}
catch (Exception e0)
{
toast = Toast.makeText(getApplicationContext(), "Could not set parameters", Toast.LENGTH_LONG);
}
...
}
}
我每次都收到 e0=NullPointer 异常。我不明白为什么。