I’m new to android, and would greatly appreciate any help. I am trying to use the background as my View, and suppose I want to create a simple Credits page with following files:
credits.xml
Credits.java
Credits.java:
public class Credits extends Activity implements OnTouchListener
{
private FrameLayout fl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fl = (FrameLayout)findViewById(R.id.credits_screen);
if ( fl == null ) Log.e("Credits", "fl is null");
else {
fl.setOnTouchListener(this);
setContentView(fl);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
}
In my credits.xml, I have:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:focusable="true" android:id="@+id/credits_screen">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:src="@drawable/credits" android:scaleType="fitXY" />
</FrameLayout>
Apparently, fl returns null at (FrameLayout)findViewById(). Although I see R.id.credits_screen was generated in the R file. May I know what is wrong with my code? Why wouldn’t it able to locate the FrameLayout?
You need to call setContentView() before findViewById().