2014-10-12 138 views
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); 
    } 

}

回答

0

新到Android?

您需要导入R.

R.java与所有你在XML的声明类似的东西,生成的文件,你需要导入它就像任何其他文件。

的位置是项目包名,所以如果你的包名是com.mycoolproject,你需要做的:

import com.mycoolproject.R; 

然后你可以参考一下布局,资源,图形等...来自你的项目。

当您编写更复杂的程序时,有时您需要不同的R's。例如,您可能需要使用android R中声明的某些东西,该东西包含默认的Android资源,如一些动画和默认样式。在这种情况下,你会引用这样的事情是这样的:

android.R.anim.push_in 

此外,如果包括库项目,每个项目库项目将有自己的[R