0
如果R.string.start引用的是strings.xml文件,那麼爲什麼它會給我錯誤「啓動無法解析或不是字段」錯誤?不應該在strings.xml文件中看到這個<string name="start">Starting...</string>
並正確編譯嗎?R.string沒有編譯
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HandlerUpdateUi!</string>
<string name="app_name">HandlerUpdateUi</string>
<string name="action">Press to Start</string>
<string name="start">Starting...</string>
<string name="first">First Done</string>
<string name="second">Second Done</string>
</resources>
MainActivity.java
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView av; //UI reference
int textString = R.string.start;
int backgroundColor = Color.DKGRAY;
final Handler mHandler = new Handler();
// Create runnable for posting results to the UI thread
final Runnable mUpdateResults = new Runnable() {
public void run() {
av.setText(textString);
av.setBackgroundColor(backgroundColor);
}
};@
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
av = (TextView) findViewById(R.id.computation_status);
Button actionButton = (Button) findViewById(R.id.action1);
actionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
doWork();
}
});
}
//example of a computationally intensive action with UI updates
private void doWork() {
Thread thread = new Thread(new Runnable() {
public void run() {
textString = R.id.start;
backgroundColor = Color.DKGRAY;
mHandler.post(mUpdateResults);
computation(1);
textString = R.id.first;
backgroundColor = Color.BLUE;
mHandler.post(mUpdateResults);
computation(2);
textString = R.id.second;
backgroundColor = Color.GREEN;
mHandler.post(mUpdateResults);
}
});
thread.start();
}
final static int SIZE = 1000; //large enough to take some time
double tmp;
private void computation(int val) {
for (int ii = 0; ii < SIZE; ii++)
for (int jj = 0; jj < SIZE; jj++)
tmp = val * Math.log(ii + 1)/Math.log1p(jj + 1);
}
}