2017-10-15 100 views
-4

我想從edittext獲取輸入,然後將其連接到給定的url。我有兩個edittext從哪裏輸入,然後添加到網址。 我的代碼如下:引導我連接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(); 

    } 
}} 

但這不起作用。我不知道是什麼問題。這是拋出錯誤,我的應用程序崩潰。請幫忙。我正在嘗試使用JSoup進行網絡報廢。日誌如下:

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); String fuel = t.getText()。toString(); EditText text =(EditText)findViewById(R.id.editText2);'在onCreate – Raghunandan

回答

0

試試這個。

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

我這樣做了,但在此之後,我無法連接字符串url '私人字符串URL =「http://www.checkpetrolprice。 com/Current /「+ fuel +」 - price-in - 「+ city +」。php「;' **燃料**和**城市**爲紅色並且表示'無法解析符號'。 –

+0

您可以再次查看。@ DibyajyotiPandey – KeLiuyue

0

爲例外說,你的看法是空這將您的字符串爲空。 你應該把你的觀點intialization在onCreate方法,即

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

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