package com.commonsware.android.layouts;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
public class NowRedux1 extends Activity implements View.OnClickListener {
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
**btn=(Button)findViewById(R.id.button);///Error**
btn.setOnClickListener(this);
updateTime();
}
public void onClick(View view){
updateTime();
}
private void updateTime(){
btn.setText(new Date().toString());
}
}
这是我的main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
这是我的R.java文件
package com.commonsware.android.layouts;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
请发送补充
答案 0 :(得分:2)
正如我从您的格式中可以看出,main.xml
不包含任何按钮。你必须添加一个带有id按钮的按钮。