【问题标题】:How to put AlertDialog in static method如何将 AlertDialog 放在静态方法中
【发布时间】:2014-04-17 09:24:29
【问题描述】:

如何在静态方法中显示警报对话框,我正在尝试设置一个条件,在该条件下我正在检查 SD 卡内的文件夹,如果存在则列出项目,否则我想显示 AlertDialog - 带有消息没有文件夹发现有教会名称

  public static List <String> fromSDCard()
    {
        List <String> listChurchWall = new ArrayList <String>();
        // listing Wallpaper using church names
        String string = "/mnt/sdcard/Church/Wallpaper/";
        f = new File (string+name+"/");
        if (f.exists()) 
         {
            files = f.listFiles ();
         }else{
        // here i want to put AlertDialog       
         }                          
    return listChurchWall;  
    }   

【问题讨论】:

  • ChurchWallpaperActivity.java 的第 226 行是什么?
  • 先修复空指针异常:TimSort.java:169...169处是什么
  • 感谢我愚蠢的错误造成的一切:(

标签: java android static android-alertdialog


【解决方案1】:

将您的应用上下文传递给静态方法。

public static List <String> fromSDCard(Context context)
{
    List <String> listChurchWall = new ArrayList <String>();
    // listing Wallpaper using church names
    String string = "/mnt/sdcard/Church/Wallpaper/";
    f = new File (string+name+"/");
    if (f.exists()) 
     {
        files = f.listFiles ();
     }else{
         // 1. Instantiate an AlertDialog.Builder with its constructor
         AlertDialog.Builder builder = new AlertDialog.Builder(context);

         // 2. Chain together various setter methods to set the dialog characteristics
         builder.setMessage(R.string.dialog_message)
         .setTitle(R.string.dialog_title);

         // 3. Get the AlertDialog from create()
        AlertDialog dialog = builder.create();

        // 4. Show the dialog
        dialog.show()
     }                          
     return listChurchWall;  
}   

如果从您的活动中调用。

public MyActivity extends Activity
{
    ....

    private void Method()
    {
         List<String> list = fromSdCard(this);
    }

    ....
}

【讨论】:

  • 现在更混乱了,这里用什么 :churchWallList = fromSDCard(?);就像我还在使用这个:churchWallList = fromSDCard();并获取 :: ChurchWallpaperListActivity 类型中的 fromSDCard(context) 方法不适用于参数 ()
  • 刚刚更新。如果您将您的课程放在您的问题中,我会更新我的答案以更具体地针对您的问题。
  • 谢谢你的回答我认为有用并且被接受了:)
【解决方案2】:
public static List<String> fromSDCard(Context mContext) {
        List<String> listChurchWall = new ArrayList<String>();
        // listing Wallpaper using church names
        String string = "/mnt/sdcard/Church/Wallpaper/";
        f = new File(string + name + "/");
        if (f.exists()) {
            files = f.listFiles();
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

            int imageResource = android.R.drawable.stat_sys_warning;
            Drawable image = mContext.getResources().getDrawable(imageResource);

            builder.setTitle("title").setMessage("your Message").setIcon(image).setCancelable(false).setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            });

            AlertDialog alert = builder.create();
            alert.setCancelable(false);
            alert.show();
        }
        return listChurchWall;
    }

【讨论】:

    【解决方案3】:

    试试下面的方法--

    public static List <String> fromSDCard(Activity a, String title, String message)
     {
        List <String> listChurchWall = new ArrayList <String>();
        // listing Wallpaper using church names
        String string = "/mnt/sdcard/Church/Wallpaper/";
        f = new File (string+name+"/");
        if (f.exists()) 
        {
          files = f.listFiles ();
        }
        else
        {
          AlertDialog.Builder dialog = new AlertDialog.Builder(a);
          dialog.setTitle(title);
          dialog.setMessage(message);
          dialog.setNeutralButton("OK", null);
          dialog.create().show();      
        }                          
        return listChurchWall;  
     }   
    

    那么在你的课堂上做---

    public MyActivity extends Activity
    {
        ....
    
        private Method()
        {
             List<String> list = fromSdCard(this, "Your Title", "Your message");
        }
    
        ....
    }
    

    更新:

    你得到一个NullPointerException,因为有些东西不应该是空的。它发生在对数组进行排序时,因此数组元素之一可能为空。看看如何为数组赋值。

    也许在它的顶部,看看Object o1Object o2 本身是否有任何对象为空。

    【讨论】:

    • 谢谢你的回答很有用:) NPE 是我的愚蠢错误的结果
    猜你喜欢
    • 2011-03-29
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多