Jawad Amjad
Jawad Amjad

Reputation: 2552

Null Pointer Exception Handling Button from XML in Library Project

I am trying to make a custom dialog in my Library Project via XML.

Here is the XML File:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/tenlogix" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="25dp" >

        <Button
            android:id="@+id/likeitb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/likeitd" />

        <Button
            android:id="@+id/dontlikeitb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/dontliked" />

        <Button
            android:id="@+id/notnowb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/notnowd" />
    </LinearLayout>

</RelativeLayout>

In Simple Java Class I am creating Dialog Like this:

final Dialog feed_back_dialog = new Dialog(mAct);

feed_back_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
feed_back_dialog.setCancelable(true);
feed_back_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

feed_back_dialog.setContentView(R.layout.ratedialog);


Button LikeIt = (Button) mAct.findViewById(R.id.likeitb);
Button DontLikeIt = (Button) mAct.findViewById(R.id.dontlikeitb);
Button NotNow = (Button) mAct.findViewById(R.id.notnowb);


LikeIt.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    String url = "market://details?id="+mAct.getPackageName();
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    mAct.startActivity(i);
    mAct.finish();
  }
});

If I don't set click listener of LikeIt button the dialog is displayed, but when I try to set a click listener on it. It's giving me a null pointer exception. I am unable to find a solution for this. The LikeIt button is null when I try to access it. I dont have any same name resources. Please help.

The reference mAct is the activity from another project in which library project is being used. Looking forward for a positive and quick response. Thanks.

Upvotes: 0

Views: 85

Answers (3)

M.Mohsin
M.Mohsin

Reputation: 314

Just change mAct in these lines of code with feed_back_dialog
As feed_back_dialog is your current layout

Button LikeIt = (Button) mAct.findViewById(R.id.likeitb); Button DontLikeIt = (Button) mAct.findViewById(R.id.dontlikeitb); Button NotNow = (Button) mAct.findViewById(R.id.notnowb);

It must be like this

Button LikeIt = (Button) feed_back_dialog.findViewById(R.id.likeitb); Button DontLikeIt = (Button) feed_back_dialog.findViewById(R.id.dontlikeitb); Button NotNow = (Button) feed_back_dialog.findViewById(R.id.notnowb);

Upvotes: 1

Rakhita Wickramatunge
Rakhita Wickramatunge

Reputation: 4503

What you can do is you can inflate the layout like this. Then with the use of the inflated view, you can get the Button controls. [I hope mAct is an Activity object]

    LayoutInflater li = LayoutInflater.from(mAct);
    View inflatedView = li.inflate(R.layout.ratedialog, null, false);

    //button initialization
    Button LikeIt = (Button) inflatedView .findViewById(R.id.likeitb);
    Button DontLikeIt = (Button) inflatedView .findViewById(R.id.dontlikeitb);
    Button NotNow = (Button) inflatedView .findViewById(R.id.notnowb);

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

Change to

 Button LikeIt = (Button) feed_back_dialog.findViewById(R.id.likeitb);

assuming the dialog layout has a button with the id likeitb

Similarly for other views

 Button DontLikeIt = (Button) feed_back_dialog.findViewById(R.id.dontlikeitb);
 Button NotNow = (Button) feed_back_dialog.findViewById(R.id.notnowb);

findViewById looks for a view with the id mentioned in the current inflated layout. So you need to use the dialog object to initialize views coz you set the content of the layout to your dialog. The views belong to the dialog

Upvotes: 1

Related Questions