What might be the reasons why the button is NULL?
Button press;
//...
press = (Button) findViewById (R.id.enter);
if (press != null)
press.setOnClickListener(this);
I checked the manifest, the xml file, the code… I don’t have any duplicate of R.id.enter, still the button is NULL… It all happened after I introduced a new Activity…
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
press = (Button) findViewById (R.id.enter);
if (press != null)
press.setOnClickListener(this);
}
the XML here (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"
android:background="@drawable/gift2"
>
<Button
android:id="@+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="415dp"
android:background="@drawable/pattern"
android:layout_gravity="center_horizontal"
android:text="Open your box now!"
android:textSize="20sp"
/>
</LinearLayout>
One possibility is that the auto-generated
R.javafile has somehow got out of sync or has invalid values in it.In this case,
R.id.entermay no longer be valid and as a resultfindViewById(R.id.enter)can no longer find a valid object to return.If using Eclipse, try using “Project -> Clean…” – this will delete all auto-generated files and re-create them.