Java Android对话框未显示

Java Android对话框未显示,java,android,android-layout,dialog,Java,Android,Android Layout,Dialog,谁会想到要显示一个对话框会如此困难。在C#中,这很容易。我到处寻找帮助,每一个提示似乎都给我带来了麻烦。有人能看一下下面的代码,告诉我哪里出了问题吗 我想要的很简单。用户点击按钮。然后列表出现。然后用户单击列表中的一个项目,出现对话框。listview事件中onItemClick中的代码来自其他人。这显然对那个人有效。我一直在点击,什么也看不到。我愿意接受关于如何修复它的建议,或者一起使用新代码。抱歉,日志中没有错误消息 请具体说明我把代码放在哪里,因为我是一个安卓noob。提前谢谢你 publ

谁会想到要显示一个对话框会如此困难。在C#中,这很容易。我到处寻找帮助,每一个提示似乎都给我带来了麻烦。有人能看一下下面的代码,告诉我哪里出了问题吗

我想要的很简单。用户点击按钮。然后列表出现。然后用户单击列表中的一个项目,出现对话框。listview事件中onItemClick中的代码来自其他人。这显然对那个人有效。我一直在点击,什么也看不到。我愿意接受关于如何修复它的建议,或者一起使用新代码。抱歉,日志中没有错误消息

请具体说明我把代码放在哪里,因为我是一个安卓noob。提前谢谢你

public class SetPrediction extends Activity
{
    Button btnInsrt, btnFTeam, btnSTeam;
    ArrayList<NameValuePair> nameValuePairs;
    TextView txtGameTeams;
    Bundle recdData;
    String game;

    JSONArray jArray;
    String result;
    InputStream is;
    StringBuilder sb;

    ArrayList<String> fNames;
    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_prediction);

        nameValuePairs = new ArrayList<NameValuePair>();
        txtGameTeams = (TextView) findViewById(R.id.txtGameTeams);

        recdData = getIntent().getExtras(); 
        game = recdData.getString("xxxx.xxxx.xxx");
        txtGameTeams.setText("xxxxxxx: " + game + " game.");

        btnInsrt = (Button) findViewById(R.id.btnInsert);
        btnFTeam = (Button) findViewById(R.id.btnFirstTeam);
        btnSTeam = (Button) findViewById(R.id.btnSecondTeam);

        btnFTeam.setText(removeSpaces(game.split("vs")[0]));
        btnSTeam.setText(removeSpaces(game.split("vs")[1]));

        btnSTeam.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                String result = "";
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("players", removeSpaces(game.split("vs")[1])));

                fNames = new ArrayList<String>();
                listView = (ListView) findViewById(R.id.lstPlayerForPrediction);

                //http post
                try
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://xxxxx/getTeamPlayers.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost); 
                    HttpEntity entity = response.getEntity();
                    InputStream is = entity.getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) 
                    {
                        sb.append(line + "\n");
                    }
                    is.close();

                    result=sb.toString();
                }
                catch(Exception e)
                {
                    Log.e("log_tag", "Error in http connection " + e.toString());
                }

                //parse json data
                try
                {
                    JSONArray jArray = new JSONArray(result);
                    for(int i=0;i<jArray.length();i++)
                        fNames.add(jArray.getJSONObject(i).getString("First_Name"));  

                }
                catch(JSONException e)
                {
                    Log.e("log_tag", "Error parsing data " + e.toString());
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(SetPrediction.this, android.R.layout.simple_list_item_1, fNames);
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new OnItemClickListener()
                {
                    public void onItemClick(AdapterView<?> arg0, View predictView, int item, long arg3) 
                    {
                        final CharSequence[] items = {"Online", "Away", "Do not distrub","Invisible","Offline"}; 
                        AlertDialog.Builder builder = new AlertDialog.Builder(SetPrediction.this); 


                        builder.setTitle("Change Status"); 
                        builder.setItems(items, new DialogInterface.OnClickListener() 
                        { 
                            public void onClick(DialogInterface dialog, int item) 
                            { 
                                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
                            } 
                        });

                        AlertDialog alert = builder.create(); 
                        alert.show();
                    } 
                });
            }
        });
    }


    public String removeSpaces(String s) 
    {  
        StringTokenizer st = new StringTokenizer(s," ",false);
        String t="";
        while (st.hasMoreElements()) t += st.nextElement();
        return t;
    }
public类SetPrediction扩展活动
{
按钮btnInsrt、btnFTeam、btnSTeam;
ArrayList名称值对;
TextView txtGameTeams;
束记录;
弦乐游戏;
杰索纳雷·贾雷;
字符串结果;
输入流为;
为某人做准备;
ArrayList fNames;
列表视图列表视图;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.set\u预测);
nameValuePairs=新的ArrayList();
txtGameTeams=(TextView)findViewById(R.id.txtGameTeams);
recdData=getIntent().getExtras();
game=recdData.getString(“xxxx.xxxx.xxx”);
setText(“xxxxxxx:+game+“game.”);
btnisrt=(按钮)findViewById(R.id.btnisert);
btnFTeam=(按钮)findViewById(R.id.btnFirstTeam);
btnSTeam=(按钮)findViewById(R.id.btnSecondTeam);
btnFTeam.setText(removespace(game.split(“vs”)[0]);
btnSTeam.setText(removespace(game.split(“vs”)[1]);
btnSTeam.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
字符串结果=”;
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“玩家”),移除空间(game.split(“vs”)[1]);
fNames=新的ArrayList();
listView=(listView)findViewById(R.id.lstplayerForPredition);
//http post
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://xxxxx/getTeamPlayers.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
InputStream=entity.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}
捕获(例外e)
{
e(“Log_标记”,“http连接错误”+e.toString());
}
//解析json数据
尝试
{
JSONArray jArray=新JSONArray(结果);

对于(inti=0;i尝试使用这些代码,而不是您的代码,只是一个小改动::

AlertDialog.Builder builder = new AlertDialog.Builder(SetPrediction.this).create();
builder.setTitle("Change Status"); 
builder.setItems(items, new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int item){ 
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
} 
});
builder.show();
同时导入以下内容:::

导入android.app.AlertDialog; 导入android.content.DialogInterface;

查看以下内容:

public void showAlertDialog(String title, String message, Context context)
{
    final AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.dismiss();
        }
    });

    alertDialog.show();
}

我是否将其放在类定义之外?例如,在
removeSpaces
方法之后?然后从
onItemClick
中调用
showAlertDialog
?如果我理解您希望通过将其放在单独的类中来重用此函数?如果是这样,您可以创建一个名为
MyUtil
的类并将其放在其中。您可以通过传递参数,可以从
onItemClick
中的任何活动调用它。警报消息仍然不会显示。我将您的代码放入一个类中,如下所示
public class Util extensed activity{}
,然后从
onItemClick
中的
SetPrediction
添加了
Util.showarterdialog(“testTitle”,“testMssage”,predictView.getContext()
但什么也没有出现。我几乎放弃了这个问题。经过两天的研究,似乎没有解决方案。问题在于我的程序结构。我在按钮事件中有一个listview事件,为了使事情更复杂,还涉及到活动组。我尝试过的每件事要么使程序崩溃,要么不成功打开对话框。但是您的代码在常规的、更正常的情况下工作。因此,无论如何,感谢您的帮助。@Ravi alertdialog没有Disclose方法。您从哪里得到的?这不是生成的。第3行给出了以下错误:
方法setItems(CharSequence[],new DialogInterface.OnClickListener(){})对于类型AlertDialog未定义