Phil
Phil

Reputation: 583

Android AlertDialog crashes with NullPointerException

I try to put HTML into a Listviewitem in an AlertDialog. Therefore i found some snippet and finally set all together with my AlertDialog.

AlertDialog.Builder builder = new AlertDialog.Builder(ChatActivity.this);
            builder.setTitle("Onlineliste");

            final LayoutInflater inflater = getLayoutInflater();
            final View convertView2 = (View) inflater.inflate(R.layout.costumdialog, null);
            builder.setView(convertView2);
            ListView lv = (ListView)convertView2.findViewById(R.id.customListView);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.costumdialog, names)
                    {
                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) 
                        {
                            View row;

                            if (null == convertView) {
                            row = inflater.inflate(R.layout.costumdialog, null);
                            } else {
                            row = convertView;
                            }

                            TextView tv = (TextView)row.findViewById(R.id.singlerow);
                            tv.setText(Html.fromHtml(getItem(position)));
                            //tv.setText(getItem(position));

                            return row;
                        }

                    };
                    lv.setAdapter(adapter);
            AlertDialog alert = builder.create();
            alert.show();

The problem is, that the setAdapter() forces the app to crash with NullPointerException

The xml files etc. do all exist and there is no problem with them I think.

Can anyone help me?

Edit:

    06-12 07:26:45.537: E/AndroidRuntime(28036): FATAL EXCEPTION: main
06-12 07:26:45.537: E/AndroidRuntime(28036): java.lang.NullPointerException
06-12 07:26:45.537: E/AndroidRuntime(28036):    at com.flipflopdev.epvp_aj1987_chat.ChatActivity$1$1.getView(ChatActivity.java:82)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.AbsListView.obtainView(AbsListView.java:2075)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1476)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.ListView.onMeasure(ListView.java:1339)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2141)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1206)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2666)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.os.Looper.loop(Looper.java:156)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at android.app.ActivityThread.main(ActivityThread.java:4977)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at java.lang.reflect.Method.invokeNative(Native Method)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at java.lang.reflect.Method.invoke(Method.java:511)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-12 07:26:45.537: E/AndroidRuntime(28036):    at dalvik.system.NativeStart.main(Native Method)

R.layout.costumdialog

    <?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customListView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

line 82 in ChatActivity

tv.setText(Html.fromHtml(getItem(position)));

customtextrow.xml

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

    <TextView
        android:id="@+id/singlerow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:paddingLeft="5dp">
    </TextView>

</RelativeLayout>

New code:

AlertDialog.Builder builder = new AlertDialog.Builder(ChatActivity.this);
            builder.setTitle("Onlineliste");

            final LayoutInflater inflater = getLayoutInflater();
            //final View convertView2 = (View) inflater.inflate(R.layout.costumdialog, null);
            //builder.setView(convertView2);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.costumdialog, names)
                    {
                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) 
                        {
                            View row;

                            if (null == convertView) {
                            row = inflater.inflate(R.layout.text, null);
                            } else {
                            row = convertView;
                            }

                            TextView tv = (TextView)row.findViewById(R.id.singlerow);
                            tv.setText(Html.fromHtml(getItem(position)));
                            //tv.setText(getItem(position));

                            return row;
                        }

                    };
                    AlertDialog alert = builder.create();
                    ListView lv = (ListView)alert.findViewById(R.id.customListView);
                    lv.setAdapter(adapter); 


            alert.show();

Upvotes: 1

Views: 700

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

Your costomdialog.xml does not have a TextView with id singlerow. Hence tv is null and when you call setText leads to NullPointerException.

From your comments it looks like singlerow is a layout with textview with

So Change this

ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.costumdialog, names)

to

ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.singlerow, names)

Edit:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.customtextrow.xml, names)

Complete Example

public class MainActivity extends Activity {

 String names[]={"A","B","C"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Onlineliste");

    final LayoutInflater inflater = getLayoutInflater();
    final View convertView2 = (View) inflater.inflate(R.layout.costomdialog, null);
    builder.setView(convertView2);
    ListView lv = (ListView)convertView2.findViewById(R.id.customListView);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.costomdialog, names)
            {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) 
                {
                    View row;

                    if (null == convertView) {
                    row = inflater.inflate(R.layout.customtextrow, null);
                    } else {
                    row = convertView;
                    }

                    TextView tv = (TextView)row.findViewById(R.id.singlerow);
                    tv.setText(names[position]);
                    //tv.setText(getItem(position));

                    return row;
                }

            };
            lv.setAdapter(adapter);
    AlertDialog alert = builder.create();
    alert.show();
     }
   }

The xml are the same no changes

Snap:

enter image description here

Edit 2:

why does the Html.fromHtml not work? :/ See my names ArrayList final ArrayList names = new ArrayList();
String[] splitted = response.split("\|"); for(String name:splitted) { names.add(Html.fromHtml("" + name.split("\+")1 + "").toString()); }

Split the String add it to arraylist. Set the html while setting text to textview.

Upvotes: 1

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

The problem is that you are inflating R.layout.costumdialog without any TextView you dont need to inflate ListView inside a ListView you just need to add TextView into the R.layout.costumdialog

Create a new layout called text.xml

<?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:orientation="vertical" >

    <TextView
        android:id="@+id/singlerow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

change it to this

if (null == convertView) {
     row = inflater.inflate(R.layout.text, null);

That is not the right way to set the adapter to the ListView you need to get the ListView reference from the AlertDialog to enable it to use the adapter.

example:

wrong

      ListView lv = (ListView)convertView2.findViewById(R.id.customListView);

right

   AlertDialog alert = builder.create();
   ListView lv = (ListView)alert.findViewById(R.id.customListView);

full code:

  AlertDialog.Builder builder = new AlertDialog.Builder(ChatActivity.this);
        builder.setTitle("Onlineliste");

        final LayoutInflater inflater = getLayoutInflater();
        final View convertView2 = (View) inflater.inflate(R.layout.costumdialog, null);
        builder.setView(convertView2);

        AlertDialog alert = builder.create();

        ListView lv = (ListView)alert.findViewById(R.id.customListView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.text, names)
                {
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) 
                    {
                        View row = super.getView(position, convertView, parent);
                        TextView tv = (TextView)row.findViewById(R.id.singlerow);
                        tv.setText(Html.fromHtml(getItem(position)));
                        //tv.setText(getItem(position));

                        return row;
                    }

                };
        lv.setAdapter(adapter);

        alert.show();

Upvotes: 1

sidhanshu
sidhanshu

Reputation: 354

I think the issue is with the inflater,

final View convertView2 = (View) inflater.inflate(R.layout.costumdialog, null);

you are casting the view which is not required.

try something like this

protected Dialog onCreateDialog() {
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
        return new AlertDialog.Builder(TicTacToe.this)
            //.setIconAttribute(android.R.attr.alertDialogIcon)
            .setTitle(getTitleText())
            .setView(textEntryView)
            .setPositiveButton("JOIN GAME", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    try
                    {
                        EditText et = (EditText)findViewById(R.id.username_edit);
                            playerName = et.getText().toString();
                    }
                    catch (Exception e)
                    {
                    }
                }
            })
            .create();
    return null;
}

Upvotes: -1

Related Questions