我已经创建了这些按钮,但是在(//按钮)的每一行中出现“错误:id无法解析或不是字段”的错误,并且“错误:layour无法解析或不是字段” (r.layou.main)hwever看不到我的布局keypad.xml
的任何错误keypad.Java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Buttons
one= (Button) findViewById(R.id.keypad_1);
two= (Button) findViewById(R.id.keypad_2);
three= (Button) findViewById(R.id.keypad_3);
four= (Button) findViewById(R.id.keypad_4);
five = (Button) findViewById(R.id.keypad_5);
six= (Button) findViewById(R.id.keypad_6);
seven = (Button) findViewById(R.id.keypad_7);
eight = (Button) findViewById(R.id.keypad_8);
nine = (Button) findViewById(R.id.keypad_9);
minus = (Button) findViewById(R.id.keypad_subtract);
hash = (Button) findViewById(R.id.keypad_hash);
keypad.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textSize="45sp">
</TextView>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keypad"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" >
//setting up keypad buttons one, two and three in one row
<TableRow>
<Button android:id="@+id/keypad_1"
android:text="@string/keypad_1" >
</Button>
<Button android:id="@+id/keypad_2"
android:text="@string/keypad_2" >
</Button>
<Button android:id="@+id/keypad_3"
android:text="@string/keypad_3" >
</Button>
</TableRow>
//setting up keypad buttons 4,5 and 6 in second row
<TableRow>
<Button android:id="@+id/keypad_4"
android:text="@string/keypad_4" >
</Button>
<Button android:id="@+id/keypad_5"
android:text="@string/keypad_5" >
</Button>
<Button android:id="@+id/keypad_6"
android:text="@string/keypad_6" >
</Button>
</TableRow>
//setting up keypad buttons 7,8 and 9 in third row
<TableRow>
<Button android:id="@+id/keypad_7"
android:text="@string/keypad_7" >
</Button>
<Button android:id="@+id/keypad_8"
android:text="@string/keypad_8" >
</Button>
<Button android:id="@+id/keypad_9"
android:text="@string/keypad_9" >
</Button>
</TableRow>
//setting up keypad buttons del,0,# and - in forth row
<TableRow>
<Button android:id="@+id/delete"
android:text="@string/keypad_DEL" >
</Button>
<Button android:id="@+id/keypad_0"
android:text="@string/keypad_0" >
</Button>
<Button android:id="@+id/keypad_hash"
android:text="@string/keypad_#" >
</Button>
<Button android:id="@+id/keypad_subtract"
android:text="@string/keypad_-" >
</Button>
</TableRow>
</TableLayout>
<TextView
android:id="@+id/incorrect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/incorrect" />
</LinearLayout>
答案 0 :(得分:1)
似乎项目尚未编译。右键单击项目清理项目。如果xml中没有编译错误(红色标记),则clean应该解决问题。
答案 1 :(得分:1)
是否可能发生此错误,因为您已命名字符串并且ID相同?
答案 2 :(得分:0)
您正在为布局调用错误的.xml文件:
setContentView(R.layout.main);
应该是:
setContentView(R.layout.keypad);
另外,您是否在将它们绑定到ID之前创建了onCreate方法上方的按钮?
private Button one;
private Button two;
// and the rest of them...