0

我正在尝试从 listView 中获取一个对话框。我想让用户看到一个具有 3 种字体大小的对话框,以便他可以更改应用程序的字体大小。要进入此对话框,用户必须点击 listView 中的按钮。我是这样设计的:

package com.reekapps.simplenote;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Dialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Settings extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);
    getActionBar().setTitle("Settings");



    String[] listItems = {"Colour", "Font Size",};
    ListView lv = (ListView) findViewById(R.id.settings_list);

    lv.setAdapter(new ArrayAdapter<String>
    (this, android.R.layout.simple_list_item_1, listItems));

    lv.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View
                view, int position, long id)
        {
            String[] listItems = {"Colour", "Font Size",};
            if(listItems[position].equals("Font Size"))
            {

                AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                builder.setTitle("Choose Font Size");
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }

        }
    });


    // TODO Auto-generated method stub
}

}

它没有显示任何错误,但是当我单击字体大小(进入对话框的按钮)时它会崩溃。

感谢您的时间!

4

3 回答 3

3

正如Raghunandan上面已经评论的那样,主要问题是上下文,像这样为列表视图更新您的点击侦听器

lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String[] listItems = { "Colour", "Font Size", };
                if (listItems[position].equals("Font Size")) {


                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            Settings.this );

                        // set title
                        alertDialogBuilder.setTitle("Choose Font Size");

                        // set dialog message
                        alertDialogBuilder
                            .setMessage("Click yes to exit!")
                            .setCancelable(false)
                            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, close
                                    // current activity

                                }
                              })
                            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

                            // create alert dialog
                            AlertDialog alertDialog = alertDialogBuilder.create();

                            // show it
                            alertDialog.show();


                }

            }
        });
于 2013-10-18T18:40:18.883 回答
0

您必须先创建构建器。请将您的 onItemClick 替换为:

            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String[] listItems = { "Colour", "Font Size", };
            if (listItems[position].equals("Font Size")) {

                AlertDialog builder = new AlertDialog.Builder(
                        Settings.this).create();
                builder.setTitle("Choose Font Size");
                builder.show();
            }
        }
    });
于 2013-10-18T19:52:16.007 回答
0

使用活动上下文

代替

  AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

经过

  AlertDialog.Builder builder = new AlertDialog.Builder(Settings.this);

不需要下面的onItemClick

   String[] listItems = {"Colour", "Font Size",};

改用下面的

   String name = (String) parent.getItemAtPosition(position); //get clicked item
   if(name.equals("Font Size"))
   {
于 2013-10-18T18:45:36.300 回答