Autore Topic: Variabile non utilizzabbile?  (Letto 1091 volte)

Offline StepGiordano

  • Nuovo arrivato
  • *
  • Post: 15
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    P500
  • Sistema operativo:
    Windows 7
Variabile non utilizzabbile?
« il: 30 Marzo 2012, 22:08:09 CEST »
0
 o_OSalve Ragazzi! non riesco ad utilizzare una variabile da aprire con un URL! la variabile in questione è scanResult
la voglio utilizzare qua:   webView.loadUrl("https://www.google.it/#hl=it&output=search&sclient=psy-ab&q=" + scanResult);

Qui tutta la classe =)


package ***************;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class barcodescanner extends Activity {
   private static final Intent SCAN_INTENT = new Intent("com.google.zxing.client.android.SCAN");
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainbarcode);
       
        PackageManager packageManager = this.getPackageManager();
        ResolveInfo resolveInfo = packageManager.resolveActivity(SCAN_INTENT, PackageManager.GET_RESOLVED_FILTER);
        if(resolveInfo==null) {
           ((Button)this.findViewById(R.id.cercabarcode)).setEnabled(false);
           ((TextView)this.findViewById(R.id.scanResult)).setText("Non hai installato nessuna applicazione per effettuare lo scan!!!\nScaricala qui: https://market.android.com/details?id=com.google.zxing.client.android&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5nb29nbGUuenhpbmcuY2xpZW50LmFuZHJvaWQiXQ..");
        }
    }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == Activity.RESULT_OK && requestCode == 0) {
         String scanResult = data.getStringExtra("SCAN_RESULT");
         ((TextView)this.findViewById(R.id.scanResult)).setText(String.format("Risultato dello scan: %1$s", scanResult));         
      }
      else {
         ((TextView)this.findViewById(R.id.scanResult)).setText("Operazione annullata!");
      }
   }
   
    public void onScan(View view) {
       startActivityForResult(SCAN_INTENT, 0);
    }
   
    public void onGoogle(View view) {
       final Activity activity = this;
        setContentView(R.layout.mainwebpage);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
 
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
 
                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });
 
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }
 
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
 
        webView.loadUrl("https://www.google.it/#hl=it&output=search&sclient=psy-ab&q=" + scanResult);
    }
 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            // Take care of calling this method on earlier versions of
            // the platform where it doesn't exist.
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        // This will be called either automatically for you on 2.0
        // or later, or by the code above on earlier versions of the
        // platform.
       finish();
    }
}

Offline Ricky`

  • Amministratore
  • Utente storico
  • *****
  • Post: 3489
  • Respect: +506
    • Github
    • Google+
    • rciovati
    • Mostra profilo
R: Variabile non utilizzabbile?
« Risposta #1 il: 30 Marzo 2012, 22:24:43 CEST »
0
Devi mettere la variabile scanResult come variabile di classe, così come l'hai scritta è locale di onActivityResult.

Sent from mobile phone, sorry for my brevity

Offline StepGiordano

  • Nuovo arrivato
  • *
  • Post: 15
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    P500
  • Sistema operativo:
    Windows 7
Re:Variabile non utilizzabbile?
« Risposta #2 il: 30 Marzo 2012, 22:31:14 CEST »
0
ho modificato il tutto così  ma non va... il risultato dello scan me lo da pari a 0 -.-


Codice (Java): [Seleziona]
package ************;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class barcodescanner extends Activity {
        private static final Intent SCAN_INTENT = new Intent("com.google.zxing.client.android.SCAN");
        int scanResult;
       
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainbarcode);
       
        PackageManager packageManager = this.getPackageManager();
        ResolveInfo resolveInfo = packageManager.resolveActivity(SCAN_INTENT, PackageManager.GET_RESOLVED_FILTER);
        if(resolveInfo==null) {
                ((Button)this.findViewById(R.id.cercabarcode)).setEnabled(false);
                ((TextView)this.findViewById(R.id.scanResult)).setText("Non hai installato nessuna applicazione per effettuare lo scan!!!\nScaricala qui: https://market.android.com/details?id=com.google.zxing.client.android&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5nb29nbGUuenhpbmcuY2xpZW50LmFuZHJvaWQiXQ..");
        }
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == Activity.RESULT_OK && requestCode == 0) {
                        ((TextView)this.findViewById(R.id.scanResult)).setText(String.format("Risultato dello scan: %1$s" + scanResult));                      
                }
                else {
                        ((TextView)this.findViewById(R.id.scanResult)).setText("Operazione annullata!");
                }
        }
   
    public void onScan(View view) {
        startActivityForResult(SCAN_INTENT, 0);
    }
   
    public void onGoogle(View view) {
        final Activity activity = this;
        setContentView(R.layout.mainwebpage);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
 
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
 
                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });
 
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }
 
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
 
        webView.loadUrl("https://www.google.it/#hl=it&output=search&sclient=psy-ab&q=" + scanResult);
    }
 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            // Take care of calling this method on earlier versions of
            // the platform where it doesn't exist.
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        // This will be called either automatically for you on 2.0
        // or later, or by the code above on earlier versions of the
        // platform.
        finish();
    }
}
« Ultima modifica: 30 Marzo 2012, 22:32:58 CEST da StepGiordano »

Offline StepGiordano

  • Nuovo arrivato
  • *
  • Post: 15
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    P500
  • Sistema operativo:
    Windows 7
Re:Variabile non utilizzabbile?
« Risposta #3 il: 31 Marzo 2012, 19:52:29 CEST »
0
Qualche soluzione??  o_O

Offline Trio87

  • Nuovo arrivato
  • *
  • Post: 11
  • Respect: 0
    • Mostra profilo
Re:Variabile non utilizzabbile?
« Risposta #4 il: 01 Aprile 2012, 10:34:40 CEST »
0
Non vorrei dire una ****ata, ma leggendo velocemente il tuo codice mi pare che la variabile scanResult non venga ne inizializzata ne modificata da nessuna parte.

Offline StepGiordano

  • Nuovo arrivato
  • *
  • Post: 15
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    P500
  • Sistema operativo:
    Windows 7
Re:Variabile non utilizzabbile?
« Risposta #5 il: 01 Aprile 2012, 11:31:45 CEST »
0
Nella variabile scanResult si andava a depositare il risultato dello scan di barcodescanner  :D
Qualche aiuto?

Zate

  • Visitatore
Re:Variabile non utilizzabbile?
« Risposta #6 il: 01 Aprile 2012, 11:58:31 CEST »
0
scanResult l'hai solo dichiarata come variabile, ma non le hai hai assegnato un valore.

Offline StepGiordano

  • Nuovo arrivato
  • *
  • Post: 15
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    P500
  • Sistema operativo:
    Windows 7
Re:Variabile non utilizzabbile?
« Risposta #7 il: 01 Aprile 2012, 12:02:17 CEST »
0
Ragazzi ho risolto io... Cmq Grazie per le prove di aiuto  ;-)

Zate

  • Visitatore
Re:Variabile non utilizzabbile?
« Risposta #8 il: 01 Aprile 2012, 12:09:50 CEST »
0
Prova a postare come hai risolto. A qualcuno potrebbe essere utile  ;-)

Offline StepGiordano

  • Nuovo arrivato
  • *
  • Post: 15
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    P500
  • Sistema operativo:
    Windows 7
Re:Variabile non utilizzabbile?
« Risposta #9 il: 01 Aprile 2012, 12:15:19 CEST »
0
Ecco a voi!  ;-)

Codice (Java): [Seleziona]
package It.**************;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class barcodescanner extends Activity {
        private static final Intent SCAN_INTENT = new Intent("com.google.zxing.client.android.SCAN");
       
        String scanResult;
       
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainbarcode);
       
        PackageManager packageManager = this.getPackageManager();
        ResolveInfo resolveInfo = packageManager.resolveActivity(SCAN_INTENT, PackageManager.GET_RESOLVED_FILTER);
        if(resolveInfo==null) {
                ((Button)this.findViewById(R.id.cercabarcode)).setEnabled(false);
                ((TextView)this.findViewById(R.id.scanResult)).setText("Non hai installato nessuna applicazione per effettuare lo scan!!!\nScaricala qui: https://market.android.com/details?id=com.google.zxing.client.android&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5nb29nbGUuenhpbmcuY2xpZW50LmFuZHJvaWQiXQ..");
        }
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == Activity.RESULT_OK && requestCode == 0) {
                        scanResult = data.getStringExtra("SCAN_RESULT");
                        ((TextView)this.findViewById(R.id.scanResult)).setText("Risultato dello scan:" + scanResult);                  
                }
                else {
                        ((TextView)this.findViewById(R.id.scanResult)).setText("Operazione annullata!");
                }
        }
   
    public void onScan(View view) {
        startActivityForResult(SCAN_INTENT, 0);
    }
   
    public void onGoogle(View view) {
        final Activity activity = this;
        setContentView(R.layout.mainwebpage);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
 
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
 
                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });
 
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }
 
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
 
        webView.loadUrl("https://www.google.it/search?tbm=bks&tbo=1&hl=it&q=" + scanResult);
    }
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            // Take care of calling this method on earlier versions of
            // the platform where it doesn't exist.
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        // This will be called either automatically for you on 2.0
        // or later, or by the code above on earlier versions of the
        // platform.
        finish();
    }
}

Offline cri90

  • Nuovo arrivato
  • *
  • Post: 14
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Samsung Corby GT-I5500
  • Sistema operativo:
    Ubuntu 12.10, Windows 7
Re:Variabile non utilizzabbile?
« Risposta #10 il: 24 Ottobre 2012, 16:01:34 CEST »
0
Ecco a voi!  ;-)

Codice (Java): [Seleziona]
package It.**************;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class barcodescanner extends Activity {
        private static final Intent SCAN_INTENT = new Intent("com.google.zxing.client.android.SCAN");
       
        String scanResult;
       
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainbarcode);
       
        PackageManager packageManager = this.getPackageManager();
        ResolveInfo resolveInfo = packageManager.resolveActivity(SCAN_INTENT, PackageManager.GET_RESOLVED_FILTER);
        if(resolveInfo==null) {
                ((Button)this.findViewById(R.id.cercabarcode)).setEnabled(false);
                ((TextView)this.findViewById(R.id.scanResult)).setText("Non hai installato nessuna applicazione per effettuare lo scan!!!\nScaricala qui: https://market.android.com/details?id=com.google.zxing.client.android&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5nb29nbGUuenhpbmcuY2xpZW50LmFuZHJvaWQiXQ..");
        }
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == Activity.RESULT_OK && requestCode == 0) {
                        scanResult = data.getStringExtra("SCAN_RESULT");
                        ((TextView)this.findViewById(R.id.scanResult)).setText("Risultato dello scan:" + scanResult);                  
                }
                else {
                        ((TextView)this.findViewById(R.id.scanResult)).setText("Operazione annullata!");
                }
        }
   
    public void onScan(View view) {
        startActivityForResult(SCAN_INTENT, 0);
    }
   
    public void onGoogle(View view) {
        final Activity activity = this;
        setContentView(R.layout.mainwebpage);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
 
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
 
                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });
 
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }
 
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
 
        webView.loadUrl("https://www.google.it/search?tbm=bks&tbo=1&hl=it&q=" + scanResult);
    }
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            // Take care of calling this method on earlier versions of
            // the platform where it doesn't exist.
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        // This will be called either automatically for you on 2.0
        // or later, or by the code above on earlier versions of the
        // platform.
        finish();
    }
}


Ti vorrei chiedere un favore. Siccome sono nuovo in questo ambiente e sto lavorando su questa applicazione, non è che potresti postare il la activity_main che da te sarebbe cercabarcode?