如何在静态方法中显示警报对话框,我正在尝试设置一个条件,我在其中检查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;
}
答案 0 :(得分:3)
将您的应用内容传递给静态方法。
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);
}
....
}
答案 1 :(得分:1)
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;
}
答案 2 :(得分:1)
请尝试以下方式 -
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
因为某些东西是空的,不应该是。它在排序数组时发生,因此可能其中一个数组元素为null。看看如何为数组赋值。
也许在它的顶部,查看任何对象Object o1
或Object o2
本身是否为空。