2017-10-15 29 views
-4

Je souhaite obtenir des entrées d'edittext, puis les concaténer dans une URL donnée. J'ai deux edittext d'où les entrées seront prises, puis ajoutées à l'URL. Mon code va comme ceci:Guidez-moi lors de la concaténation d'une chaîne dans l'URL

public class MainActivity extends AppCompatActivity{ 
EditText t = (EditText) findViewById(R.id.editText); 
String fuel = t.getText().toString(); 
EditText text = (EditText) findViewById(R.id.editText2); 
String city = text.getText().toString(); 
private ListView lv; 
public ArrayList list= new ArrayList(); 
private ArrayAdapter<String> adapter; 
private String URL="http://www.checkpetrolprice.com/Current/"+fuel+"-price-in-"+city+".php"; 
private ProgressDialog progressDialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    lv=(ListView)findViewById(R.id.list); 
    adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list); 

    Button btn=(Button)findViewById(R.id.button); 

    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

     new VeriGetir().execute(); 

     } 
    }); 

} 


private class VeriGetir extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     progressDialog= new ProgressDialog(MainActivity.this); 
     progressDialog.setTitle("Loading"); 
     progressDialog.setMessage("Please Wait..."); 
     progressDialog.setIndeterminate(false); 
     progressDialog.show(); 

    } 



    @Override 
    protected Void doInBackground(Void... voids) { 

     try { 
      //Document doc= Jsoup.connect(URL).timeout(30*1000).get(); 
      Document row= Jsoup.connect(URL).timeout(30*1000).get(); 

      //Elements oyunadi=doc.select("table[class=pure-table]"); 
      // Elements oyunadi = row.getElementsByTag("tbody"); 
      Elements elem=row.select("h3[title=Current price of Petrol in "+city+" today]"); 

      for (int i=0;i<elem.size();i++){ 

       list.add(elem.get(i).text()); 
      } 


     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


     return null; 
    } 


    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 

     lv.setAdapter(adapter); 
     progressDialog.dismiss(); 

    } 
}} 

Mais cela ne fonctionne pas. Je ne sais pas quel est le problème. C'est une erreur de lancer et mon application plante. S'il vous plaît aider. J'essaye d'employer le JSoup pour la mise au rebut de Web. Le journal va comme ceci:

10-15 12:09:46.4-32104/? E/AndroidRuntime: FATAL EXCEPTION: main 
                Process: com.test.ertugrulemre.htmlparsing, PID:4 
                java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.ertugrulemre.htmlparsing/com.test.ertugrulemre.htmlparsing.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2586) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751) 
                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) 
                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                 at android.os.Looper.loop(Looper.java:154) 
                 at android.app.ActivityThread.main(ActivityThread.java:6186) 
                 at java.lang.reflect.Method.invoke(Native Method) 
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 
                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 
                 at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120) 
                 at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151) 
                 at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31) 
                 at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55) 
                 at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33) 
                 at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33) 
                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201) 
                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185) 
                 at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525) 
                 at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193) 
                 at com.test.ertugrulemre.htmlparsing.MainActivity.<init>(MainActivity.java:24) 
                 at java.lang.Class.newInstance(Native Method) 
                 at android.app.Instrumentation.newActivity(Instrumentation.java:1079) 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2576) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)  
                 at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)  
                 at android.os.Handler.dispatchMessage(Handler.java:102)  
                 at android.os.Looper.loop(Looper.java:154)  
                 at android.app.ActivityThread.main(ActivityThread.java:6186)  
                 at java.lang.reflect.Method.invoke(Native Method)  
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)  
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)  
+1

'EditText t = (EditText) findViewById (R.id.editText); Chaîne de carburant = t.getText(). ToString(); EditText text = (EditText) findViewById (R.id.editText2); 'onCréer – Raghunandan

Répondre

0

Essayez ceci.

private EditText t, text; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // edited here ,add findViewById method 
    t = (EditText) findViewById(R.id.editText); 
    String fuel = t.getText().toString(); 
    text = (EditText) findViewById(R.id.editText2); 
    String city = text.getText().toString(); 
    String URL="http://www.checkpetrolprice.com/Current/"+fuel+"-price-in-"+city+".php"; 
} 
+0

Je l'ai fait, mais après cela, je ne peux pas concaténer les chaînes avec l'URL ' private String URL = "http: //www.checkpetrolprice. com/Actuel/"+ carburant +" - prix-dans - "+ ville +". php ";' ** carburant ** et ** ville ** était rouge et dit "ne peut pas résoudre le symbole". –

+0

Vous pouvez le vérifier à nouveau. @ DibyajyotiPandey – KeLiuyue

0

comme l'exception dit, votre point de vue est nulle qui transforme votre chaîne est nulle. Vous devriez obtenir votre point de vue dans la méthode onCreate initialisation, i.e.

EditText edit; 
onCreate(){ 
edit=(EditText) findViewById(R.id.edit_text); 

//Now get the text 
String city=edit.getText().toString(); 
}