可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
So I'm programming a simple calculator in Eclipse for Android 4.0 and I'm trying to stream-line my code and make it as simple as possible. The place I'm trying to clean up is my findViewById()'s. Since I have buttons 0-9 to instantiate I have a block of code ten lines long that look like this:
b0 = (Button) findViewById(R.id.b0); b1 = (Button) findViewById(R.id.b1); ... b9 = (Button) findViewById(R.id.b9);
As you can see this thing is just begging for a for-loop. So what I wanted to do was make two arrays. One instance variable array in the Activity which holds all the Button instance variables for the number pad:
private Button[] numberPad = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9};
and then another array in the id class in the R.java file that holds all the button id variables that would look like this:
private static final int[] numberPad = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9};
Sooo that I can reduce ten lines of Button instantiations to two using this loop:
for(int i = 0; i < numberPad.length; i++) numberPad[i] = (Button) findViewById(R.id.numberPad[i]);
When I type it out it's fine, but when I go to save it it reverts back automatically to it's original form. I don't see anything wrong with this code. It doesn't produce any errors as far as I can tell. Why can't I edit the R.java file in this way? Is there a way to? Should I?
回答1:
As said before, R file is (or should be) re-generated automatically on every build. You should try some dependency-injection framework (Roboguice works well for Android programming) to clean up issues with handling Views. For example instead of (code taken from project's documentation):
class AndroidWay extends Activity { TextView name; ImageView thumbnail; LocationManager loc; Drawable icon; String myName; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name = (TextView) findViewById(R.id.name); thumbnail = (ImageView) findViewById(R.id.thumbnail); loc = (LocationManager) getSystemService(Activity.LOCATION_SERVICE); icon = getResources().getDrawable(R.drawable.icon); myName = getString(R.string.app_name); name.setText( "Hello, " + myName ); }}
You can use simpler and shorter version:
class RoboWay extends RoboActivity { @InjectView(R.id.name) TextView name; @InjectView(R.id.thumbnail) ImageView thumbnail; @InjectResource(R.drawable.icon) Drawable icon; @InjectResource(R.string.app_name) String myName; @Inject LocationManager loc; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name.setText( "Hello, " + myName ); } }
This not only helps keeping your Views management clean and in-place but is also very helpful when it comes to testing.
回答2:
The R.java file is re-created on every build.
回答3:
As R.java is auto-generated, I think it is a pretty bad idea editing it.
Sometimes when using Eclipse, I get some strange errors concerning R.java, to solve this, I just clean the project, and then every is working again.
Check out this link for more information on resources: http://developer.android.com/guide/topics/resources/accessing-resources.html
回答4:
Even if you did try that approach you will have to use Reflection in order to obtain "findViewbyID" as (R.id.numberPad[i]) will now work without Reflection.
Dont use Reflection either, although it will neaten your code it is very slow.
回答5:
You know, you can implement all that outside the R.java file and in your own classes - just do a static import of R.id.*.
回答6:
R.java is a system generated file which iautomatically generated. You should not try to edit it manually.