【发布时间】:2015-09-01 05:27:16
【问题描述】:
我正在运行一个安卓服务。当我从服务中获得预期结果时,我希望更改 TextView 文本。我已经实现了一个接口方法来获取回调来更改我的 textView。接口抽象方法运行良好,但无法更改我的活动的 Textview 文本。
我在这里简化了示例代码
public class MainActivity extends Activity implements MyInterface {
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.sample_text);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Asim----", "Start service"); // print this line well
startService(new Intent(MainActivity.this, MyService.class));
}
});
}
@Override
public void testMe() {
Log.e("Asim----", "Hi"); // print this line well
mTextView.setText("New Text"); // Chrashes happens here
}
}
这里的 MyService 类
public class MyService extends Service {
@Override
public void onCreate() {
//super.onCreate();
Log.e("test1------", "onCreate"); // print this line well
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("test2------", "onStartCommand"); // print this line well
MyInterface mi = new MainActivity();
mi.testMe();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
【问题讨论】:
-
总是尝试调试你的代码,它会告诉你在这类问题中有些东西正在变得空。