我刚刚开始学习Android,我的第一个例子中有以下错误请帮助我。
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button buttonNesnesi=(Button) findViewById(id.button1);
buttonNesnesi.setOnClickListener(
new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(SecondActivity.this, "Hello!!!",1000).show();
}
}
);
}
}
例外:
E / AndroidRuntime(543):java.lang.RuntimeException:无法启动 活动 ComponentInfo {net.developersland / net.developersland.SecondActivity}: 显示java.lang.NullPointerException
这是我的main.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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
答案 0 :(得分:2)
这应该是,
final Button buttonNesnesi=(Button) findViewById(R.id.button1);
不是
final Button buttonNesnesi=(Button) findViewById(id.button1);
答案 1 :(得分:1)
您缺少 R .id.button1
final Button buttonNesnesi=(Button) findViewById(R.id.button1);