1

I'm creating a custom AlertDialog and I'm getting one error.

On strings.xml I have defined a string like:

<string name="infoDetail">Test: <a href="http://www.google.com">Google</a></string>

I've created a custom layout like this:

<?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" >

    <TextView
        android:id="@+id/infoVersionTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:gravity="right|center"
        android:text="@string/infoVersion"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/infoVersionTitleValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/infoVersionTitle"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/infoDetailText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/infoVersionTitle"
        android:autoLink="web"
        android:linksClickable="true"
        android:padding="4dp"
        android:text="@string/infoDetail" />

</RelativeLayout>

This is the code for creating the AlertDialog with my custom layout:

case R.id.info:
    AlertDialog.Builder about = new AlertDialog.Builder(this);

    about.setTitle(getString(R.string.app_name));
    about.setIcon(R.drawable.info);
    about.setView(getLayoutInflater().inflate(R.layout.info, null));


    TextView infoDetail = (TextView) findViewById(R.id.infoDetailText);
    infoDetail.setMovementMethod(LinkMovementMethod.getInstance());

    about.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();                       
        }
    });
    about.show();               
    break;

Whit this I'm getting a java.lang.NullPointerException on infoDetail.setMovementMethod(LinkMovementMethod.getInstance());.

If I remove this line my custom layout is shown as expected but the url is not clickable.

What am I doing wrong?

laalto
  • 150,114
  • 66
  • 286
  • 303
Favolas
  • 6,963
  • 29
  • 75
  • 127
  • Please see below link for make custom alert dialog [Android Custom Dialog Example](http://www.mkyong.com/android/android-custom-dialog-example/) [Android Custom Alert Dialog Example](http://www.mkyong.com/android/android-prompt-user-input-dialog-example/) – Dipak Keshariya Aug 21 '12 at 11:33
  • Thanks, but that not solves my problem of clicking on the URL – Favolas Aug 21 '12 at 14:15
  • The main problem in this line. TextView infoDetail = (TextView) findViewById(R.id.infoDetailText); – Dipak Keshariya Aug 22 '12 at 04:16
  • @DipakKeshariya Yes. You are right. But cant seem to bypass this situation. Have done `LayoutInflater li = getLayoutInflater();View infoLayout = li.inflate(R.layout.info, (ViewGroup) findViewById(R.id.infoLayout));` about.setView(infoLayout);about.setView(infoLayout);` and then `AlertDialog infoDialog = about.create(); infoDialog.show();((TextView)infoDialog.findViewById(R.id.infoDetailText)).setMovementMethod(LinkMovementMethod.getInstance());` but that does not solve my problem. Stuck at this point – Favolas Aug 22 '12 at 07:33
  • your problem is solved or not? – Dipak Keshariya Aug 22 '12 at 07:52
  • @DipakKeshariya Not solved. Managed to show the text but the links do not work. They're not even underlined – Favolas Aug 22 '12 at 08:09

3 Answers3

0

When using widgets in dialog should refer it with dialog name like

TextView nameTextView = (TextView) your_dialog_name.findViewById(R.id.textviewid);

or they will show null pointer exception so

refer the TextView like

   TextView infoDetail = (TextView) about.findViewById(R.id.infoDetailText);
Avi Kumar
  • 4,403
  • 8
  • 36
  • 67
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
  • 1
    Hi. done that but gives me an error. `The method findViewById(int) is undefined for the type AlertDialog.Builder` and the given cast options are `Activity`, `Dialog` and `View` all of which then give `Cannot cast from AlertDialog.Builder to type`. Done this: `View infoLayout = getLayoutInflater().inflate(R.layout.info, null);` `about.setView(infoLayout);` and then TextView infoDetail = (TextView) infoLayout.findViewById(R.id.infoDetailText); infoDetail.setMovementMethod(LinkMovementMethod.getInstance());`. Now it shows but cannot click link. It also does not appear underlined in blue. – Favolas Aug 21 '12 at 14:14
0

have you tried

TextView textview = (TextView) getView().findViewById(R.id.textview);

aswell?

bofredo
  • 2,348
  • 6
  • 32
  • 51
0

Replace

about.setView(getLayoutInflater().inflate(R.layout.info, null));

TextView infoDetail = (TextView) findViewById(R.id.infoDetailText);

with

View v = getLayoutInflater().inflate(R.layout.info, null);
about.setView(v);
TextView infoDetail = (TextView)v.findViewById(R.id.infoDetailText);

Because you will need to call findViewById() on the dialog's view hierarchy, not on your activity's. A view with the specified id does not exist in the activity hierarchy and a null is returned. Attempting to invoke a method (setMovementMethod) on null causes the NPE.

(Yeah, I know, old question but got random-bumped as unanswered by Community and it was easy to answer.)

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303