Search code examples
androiddialogcustomdialog

Android custom dialog force close issue


I'm trying to implement a custom dialog which contains a ListView. The objective is to display the dialog on a button click. For this, I'm creating a custom layout for my dialog and setting the content view on button click. For some unknown reason, the app is getting force closed. Can anybody tell me what am I doing wrong? Here are the code snippets: Main Activity:

package com.example.zdialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String[] myList={"book", "table", "car", "bag", "fridge",
                "television", "radio"};
        Button btn=(Button)findViewById(R.id.button1);
        ListView lv=(ListView)findViewById(R.id.listView1);
        ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList);
        lv.setAdapter(aa);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Dialog dialog=new Dialog(MainActivity.this);
                dialog.setContentView(R.layout.dialog_layout);
                dialog.show();

            }
        }); 
    }
}

activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="17dp"
        android:text="Show Dialog" />

</RelativeLayout>

dialog_layout.xml:

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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

As you can see, the custom dialog contains a simple ListView. I know, there are other methods for implementing list based dialogs, but I just want to know what's wrong with this code (learning Android programming on my own). Please help!


Solution

  • ListView is not inside R.layout.activity_main so it gives you null

    You need change these line

        ListView lv=(ListView)findViewById(R.id.listView1);
        ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myList);
        lv.setAdapter(aa);
    

    here it gives you NullPointerException..

    so you can change it like inside your onClick() method..

        Dialog dialog=new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.dialog_layout);
        ListView lv=(ListView)dialog.findViewById(R.id.listView1);
        ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList);
        lv.setAdapter(aa);
        dialog.show();