为少数编辑文本分配 id 会导致以下错误:
the error is in the parameters of findViewById token
这是它所在的代码行
final EditText myEditField = (EditText) findViewById(R.id.percentText1);
...这是 Xml 代码,再次对发布 java 的书呆子感到抱歉...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/percentText1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="224dp"
android:onClick="onEnterClick"
android:text="Calculate Tip" />
<EditText
android:id="@+id/percentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp"
android:ems="10"
android:singleLine="true"
android:text="Percent" />
<EditText
android:id="@+id/percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/percentText"
android:layout_alignParentTop="true"
android:ems="10"
android:singleLine="true"
android:text="Bill Amount" >
<requestFocus />
</EditText>
</RelativeLayout>
...Java代码...我认为的错误是我不正确地给编辑文本一个id ...
package tip.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import tip.calculator.R;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText myEditField = (EditText) findViewById(R.id.percentText); //error here
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onEnterClick(String args []){
int percentage;
int percentageInp;
int billAmount;
}
}