Frage

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!

War es hilfreich?

Lösung

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();

Andere Tipps

ListView is inside Dialog Layout but you are trying to get it from Activity layout so change your code as to work:

    btn.setOnClickListener(new View.OnClickListener() {

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

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

            dialog.show();

        }
    }); 

As I can see from your above code, your ListView resides inside your dialog_layout.xml which you are setting as custom dialog background.But you are accessing it wrongly,

change your button click to following,

btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                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();

            }
        }); 

And delete the ListView initialization that you have done earlier.

You can not set the data into the ListView first and then open the dialog. Set the data into ListView before showing the Dialog and access the Listview with respect to the instance of the Dialog only.

Try out as below:

btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            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>(MainActivity.this, android.R.layout.simple_list_item_1, myList);
            lv.setAdapter(aa);
            dialog.show();

        }
    }); 

you need to change because your ListView

  ListView lv=(ListView)findViewById(R.id.listView1);

is in your dialog_layout.xml: so change it

  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(); 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top