-1

場所が変わった後に実行するクラスを呼び出すためのガイドが必要です。誰か助けてくれませんか。問題はこのコードが原因だと思います:

Intent i = new Intent(getApplicationContext(), CreateNewProduct.class);
startActivity(i);

これはコード全体です:

(これは、場所が変わったときにいくつかの条件を設定する場所です。また、これは、以下の次のクラスを呼び出す必要がある部分でもあります。)

class myLocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                double pLong = location.getLongitude();
                double pLat = location.getLatitude();
                textLat.setText(Double.toString(pLat));
                textLong.setText(Double.toString(pLong));

                Intent i = new Intent(getApplicationContext(), CreateNewProduct.class);
                startActivity(i);
            }
        }
        @Override
        public void onProviderDisabled(String provider) {
        }
        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

(呼び出されるクラス)

class CreateNewProduct extends AsyncTask<String, String, String> {
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Sample.this);
            pDialog.setMessage("Sending Location");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String latitude = textLong.getText().toString();
            String longitude = textLat.getText().toString();
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("latitude", latitude));
            params.add(new BasicNameValuePair("longitude", longitude));
            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);
            // check log cat fro response
            Log.d("Create Response", json.toString());
            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Toast.makeText(getBaseContext(), "Success",
                            Toast.LENGTH_SHORT).show();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }
    }
4

3 に答える 3

2

クラスCreateNewProductはActivityではありません。IntentおよびstartActivity()メソッドを使用して開始することはできません。最善の解決策は、このクラスのオブジェクトを作成し、このクラスからメソッドを呼び出すことです。

コードサンプル:

@Override

        public void onLocationChanged(Location location) {

            if (location != null) {

                double pLong = location.getLongitude();

                double pLat = location.getLatitude();

                textLat.setText(Double.toString(pLat));

                textLong.setText(Double.toString(pLong));

               CreateNewProduct p = new CreateNewProduct();
               p.execute();

            }
        }

お役に立てば幸いです。さらに質問がある場合は、質問してください!

于 2013-01-22T09:17:11.937 に答える
2

aynctask を次のように実行する必要があります。

new CreateNewProduct().execute();

startActivity() は名前が示すようにアクティビティを開始するためのものです

于 2013-01-22T09:11:38.000 に答える
0

クラスを呼び出す代わりに、LocationManagerクラスを使用して、目的の場所の近接度を追加できます。

addProximityAlert()メソッドとPendingIntentを使用して実装できます。

場所の変更時に実行されるコードをPendingIntentに配置します。

Androidプロキシミティアラート

于 2013-01-22T11:19:04.873 に答える