Android: findViewbyID undefined
I'm a beginner on Android programming, and get stuck when following the code from a book. I checked another threads but I couldn't find a solution. Hope you can help me.
My code:
karmind_test.java
package karmind.com.karmind_test;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
public class Karmind_test extends Activity开发者_如何学运维 implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View about_button = findViewbyID(R.id.about_button);
about_button.setOnClickListener(this);
View exit_button = findViewbyID(R.id.exit_button);
exit_button.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
// More buttons here
}
}
}
main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:text="@string/menu"
android:textSize="24.5sp"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id= "@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about"/>
<Button
android:id= "@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit"/>
<TextView
android:text="@string/copyright"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
The error I got twice:
The method findViewbyID(int) is undefined for the type Karmind_test
Thanks for helping me out.
Java is case sensitive. The method name is findViewById
, not findViewByID
.
精彩评论