Android 指导我在url中连接字符串

Android 指导我在url中连接字符串,android,nullpointerexception,Android,Nullpointerexception,我想从edittext获取输入,然后将其连接到给定的url。我有两个edittext,从中获取输入,然后添加到url。 我的代码是这样的: public class MainActivity extends AppCompatActivity{ EditText t = (EditText) findViewById(R.id.editText); String fuel = t.getText().toString(); EditText text = (EditText) findViewB

我想从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();

    }
}}
10-15 12:09:46.382 32104-32104/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.test.ertugrulemre.htmlparsing, PID: 32104
                                                   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) 
public类MainActivity扩展了AppCompatActivity{
EditText t=(EditText)findViewById(R.id.EditText);
字符串fuel=t.getText().toString();
EditText=(EditText)findViewById(R.id.editText2);
字符串city=text.getText().toString();
私有ListView lv;
public ArrayList list=new ArrayList();
专用阵列适配器;
专用字符串URL=”http://www.checkpetrolprice.com/Current/“+fuel+”-价格单位-“+city+”.php”;
私有进程对话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.list);
adapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,list);
按钮btn=(按钮)findViewById(R.id.Button);
btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
新的VeriGetir().execute();
}
});
}
私有类VeriGetir扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=新建progressDialog(MainActivity.this);
progressDialog.setTitle(“加载”);
progressDialog.setMessage(“请稍候…”);
progressDialog.setUndeterminate(false);
progressDialog.show();
}
@凌驾
受保护的空位背景(空位…空位){
试一试{
//Document doc=Jsoup.connect(URL).timeout(30*1000.get();
Document row=Jsoup.connect(URL).timeout(30*1000.get();
//元素oyunadi=doc.select(“表[类=纯表]”);
//Elements oyunadi=row.getElementsByTag(“tbody”);
Elements elem=行。选择(“h3[标题=当前“+城市+”汽油价格]”;
对于(inti=0;i试试这个

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";
}

正如异常所说,视图为null,而字符串则为null。 您应该在onCreate方法中初始化视图,即

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

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

EditText t=(EditText)findViewById(R.id.EditText);String fuel=t.getText().toString();EditText text=(EditText)findViewById(R.id.editText2);
在创建时我这样做了,但在那之后我无法用url
私有字符串url=连接字符串http://www.checkpetrolprice.com/Current/“+fuel+”-价格单位-“+city+”.php”
fuel and city是红色的,上面写着“无法解析符号”。您可以再次检查。@DibyajyotiPandey