Reputation: 5259
It should be pretty straightforward but something is wrong.
public class CreditView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
setContentView(R.layout.creditlayout);
back = (TableRow) findViewById(R.id.backView);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
fbBtn = (ImageButton) findViewById(R.id.fbBtn);
fbBtn.setOnClickListener(new OnClickListener() {
....
I am getting a null pointer on fbBtn.
and the layout :
<?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:background="@drawable/backgraund"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/topbar" >
<TableRow
android:id="@+id/backView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:clickable="true"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text="Back"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="normal" />
</TableRow>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/roadmania" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/TopLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation = "vertical"
android:gravity="center"
android:padding="5dip" >
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/twitterBtn"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/twitter_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fbBtn"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/fb_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
</TableRow>
</RelativeLayout>
Upvotes: 1
Views: 1273
Reputation: 126455
Change your ImageButtons
from:
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/twitterBtn"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/twitter_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fbBtn"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/fb_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
to
<ImageButton
android:id="@+id/twitterBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/twitter_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
<ImageButton
android:id="@+id/fbBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/fb_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
You have missed the ids and thats why you have a NullPointerException
:
fbBtn = (ImageButton) findViewById(R.id.fbBtn);
fbBtn
is null!
Upvotes: 1
Reputation: 8338
You accidentally didn't correctly define the id. You probably copied and pasted, but forget to change layout_below
to id
.
You have this:
android:layout_below="@+id/fbBtn"
Change that to this:
android:id="@+id/fbBtn"
Upvotes: 3