Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在AlertDialog中调用外部方法?_Android - Fatal编程技术网

Android 如何在AlertDialog中调用外部方法?

Android 如何在AlertDialog中调用外部方法?,android,Android,我在AppTools.java中有一个连接HttpGet的方法: public class AppTools extends Activity { public void connectToServerWithURL(String URL) throws ClientProtocolException, IOException, JSONException { /* Start connect */ new Thread() { client = new DefaultH

我在AppTools.java中有一个连接HttpGet的方法:

public class AppTools extends Activity {
public void connectToServerWithURL(String URL)
        throws ClientProtocolException, IOException, JSONException {
/* Start connect */
    new Thread() {
client = new DefaultHttpClient();
request = new HttpGet(URL);
response = client.execute(request);
reader = new BufferedReader(new InputStreamReader(response.getEntity()
            .getContent()));
builder = new StringBuilder();
    for (String s = reader.readLine(); s != null; s = reader.readLine()) {
        builder.append(s);
    }

if (builder != null) {
        /* Transfer to JSONArray */
        jsonTransfer = new JSONObject(builder.toString());
        systemConfigJSONArray = jsonTransfer.getJSONArray(config);
        runOnUiThread(performResult);
    }
       }.start();
} 
private Runnable performResult = new Runnable() {
    public void run() {
        closeProgressDialog();
        performResult(systemConfigJSONArray);
    }
};

/** Connect complete, interface for Override **/
public void performResult(JSONArray resultArray) {
}
另外,在另一个活动中是扩展AppTools:

public class A extends AppTools { 
在活动A中,有一个AlertDialog用于选择要连接的url:

new AlertDialog.Builder(this).setView(aLayout)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int        which) {
                    /* Set command */
                    if (spinner_options.getSelectedItemPosition() == 0) {
                        connectToServerWithURL("http://...");
                    } else if (spinner_options
                            .getSelectedItemPosition() == 1) {
                        connectToServerWithURL("http://...");
                    }
                }
            }).setNegativeButton("Cancel", null).show();
.setView(aLayout)
中的aLayout有一个微调器,因此setPositiveButton的onClick接口

将获取微调器的选定位置并执行该方法

但是代码无法工作,LogCat显示AlertController$ButtonHandler错误

问题出在哪里?

//在OnCreate()方法之前定义字符串
//Define String before OnCreate() method

String url;


new AlertDialog.Builder(this).setView(aLayout)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int        which) {
                    /* Set command */
                    if (spinner_options.getSelectedItemPosition() == 0) {
                        url="http://...";
                         new YourAsyncTask().execute();
                      //  connectToServerWithURL("http://...");

                    } else if (spinner_options
                            .getSelectedItemPosition() == 1) {
                         url="http://...";
                         new YourAsyncTask().execute();
                       // connectToServerWithURL("http://...");
                    }
                }
            }).setNegativeButton("Cancel", null).show();


 class YourAsyncTask extends AsyncTask<Void, Void, Void>
    {

        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute()
        {
            //show your dialog here
            progressDialog = ProgressDialog.show(yourActivity.this,"Please wait...", "Loading  ...", true);
        }

        @Override
        protected Void doInBackground(Void... params)
        {        
            //make your request here - it will run in a different thread
            try
            {

               connectToServerWithURL(url);  

            }
            catch (Exception e)
            {
                // TODO: handle exception
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {


                progressDialog.dismiss();


        }
字符串url; 新建AlertDialog.Builder(this.setView)(aLayout) .setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){ @凌驾 public void onClick(DialogInterface dialog,int which){ /*设置命令*/ 如果(微调器选项。getSelectedItemPosition()==0){ url=”http://..."; 新建YourAsyncTask().execute(); //connectToServerWithURL(“http://..."); }else if(微调器选项) .getSelectedItemPosition()=1){ url=”http://..."; 新建YourAsyncTask().execute(); //connectToServerWithURL(“http://..."); } } }).setNegativeButton(“取消”,null).show(); 类YourAsyncTask扩展了AsyncTask { 私有进程对话; @凌驾 受保护的void onPreExecute() { //在这里显示您的对话框 progressDialog=progressDialog.show(yourActivity.this,“请稍候…”,“正在加载…”,true); } @凌驾 受保护的Void doInBackground(Void…参数) { //在此处提出请求-它将在不同的线程中运行 尝试 { connectToServerWithURL(url); } 捕获(例外e) { //TODO:处理异常 } 返回null; } @凌驾 受保护的void onPostExecute(void结果) { progressDialog.disclose(); }
删除类A中的if条件,只写connectToSeverWithUrl方法如果有效,那么你的Spinner就有问题抱歉,我忘记了connectToServerWithURL方法中的一些代码,它将在线程中运行,因此可能不需要使用AsyncTask