Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中按下按钮时从文本框读取_Java_Android_Button_Textedit - Fatal编程技术网

Java 在android中按下按钮时从文本框读取

Java 在android中按下按钮时从文本框读取,java,android,button,textedit,Java,Android,Button,Textedit,我试图在按下按钮时检索文本框值,但它不起作用。这是我的密码。有什么想法吗 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.httpxml); httpstuff = (TextView) findViewById(R.id.http); client = new DefaultHttp

我试图在按下按钮时检索文本框值,但它不起作用。这是我的密码。有什么想法吗

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpxml);
    httpstuff  = (TextView) findViewById(R.id.http);
    client = new DefaultHttpClient();
    button = (Button)findViewById(R.id.shoppingprice);
    button.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

                //shoppingapi price =  new shoppingapi();
                et=(EditText)findViewById(R.id.text);
                txt=et.getText().toString();

            }

        });


   new Read().execute("displayprice");
}

@SuppressLint("ShowToast")
public JSONObject productprice(String productname) throws ClientProtocolException,IOException,JSONException
{

    StringBuilder url = new StringBuilder(URL);
    url.append(productname);
    url.append("&searchType=keyword&contentType=json");

    HttpGet get = new HttpGet(url.toString());

    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();

    Log.d("Price", "asdasd");

    if(status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
         jObj = new JSONObject(data);
        JSONObject jsonData = jObj.getJSONObject("mercadoresult");
        JSONObject jsonProducts = jsonData.getJSONObject("products");
        JSONArray jsonArray = jsonProducts.getJSONArray("product");
        jsonArray = (JSONArray) jsonArray.get(1);

        jObj = (JSONObject)jsonArray.get(0);

        return jObj;
        }
    else
    {
    Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();

    return null;    

  }
}

public class Read extends AsyncTask<String,Integer,String>
{


    @Override
    protected String doInBackground(String... params) {

        try {

            json = productprice(txt); 

            return json.getString("displayprice");

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        }

        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        //super.onPostExecute(result);

        //httpstuff.setText("The price of the Product is ");
        httpstuff.setText(result);
        httpstuff.setText(txt);
    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.httpxml);
httpstuff=(TextView)findViewById(R.id.http);
client=新的DefaultHttpClient();
按钮=(按钮)findViewById(R.id.shoppingprice);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
//shoppingapi价格=新shoppingapi();
et=(EditText)findViewById(R.id.text);
txt=et.getText().toString();
}
});
新建Read().execute(“displayprice”);
}
@SuppressLint(“ShowToast”)
公共JSONObject productprice(String productname)抛出ClientProtocolException、IOException、JSONException
{
StringBuilder url=新的StringBuilder(url);
url.append(产品名称);
append(“&searchType=keyword&contentType=json”);
HttpGet=newhttpget(url.toString());
HttpResponse r=client.execute(get);
int status=r.getStatusLine().getStatusCode();
日志d(“价格”、“asdasd”);
如果(状态==200){
HttpEntity e=r.getEntity();
字符串数据=EntityUtils.toString(e);
jObj=新的JSONObject(数据);
JSONObject jsonData=jObj.getJSONObject(“mercadoresult”);
JSONObject jsonProducts=jsonData.getJSONObject(“产品”);
JSONArray JSONArray=jsonProducts.getJSONArray(“产品”);
jsonArray=(jsonArray)jsonArray.get(1);
jObj=(JSONObject)jsonArray.get(0);
返回jObj;
}
其他的
{
Toast.makeText(MainActivity.this,“error”,Toast.LENGTH_LONG.show();
返回null;
}
}
公共类读取扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
json=产品价格(txt);
返回json.getString(“displayprice”);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
//e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
//e、 printStackTrace();
}捕获(JSONException e){
//TODO自动生成的捕捉块
//e、 printStackTrace();
}
//TODO自动生成的方法存根
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
//super.onPostExecute(结果);
//httpstuff.setText(“产品价格为”);
httpstuff.setText(结果);
httpstuff.setText(txt);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
}

它不显示错误,但将
txt
值显示为空白

按钮外引用它
单击

 et=(EditText)findViewById(R.id.text);
直接访问下面显示的
AsynTask
内部

public class Read extends AsyncTask<String,Integer,String>
{

因为你在打这个电话

new Read().execute("displayprice");
立即创建

其中,当您单击按钮时,as txt值正在更改

因此,在分配值之前,您正在访问
txt
值。如果您想使用这样的值更改并尝试这样做

public void onClick(View arg0) {

            et=(EditText)findViewById(R.id.text);
            txt=et.getText().toString();

            new Read().execute("displayprice");

        }

    });

您是否收到任何错误?您已检索文本并将其存储在
txt
变量中。但是你没有用这个
txt
变量做任何事情。在其他部分中,我尝试显示它为空白。你的意思是像触摸一样按下或单击(我看到人们的意思是触摸而不是单击)。没有,没有错误,但稍后在代码中我尝试显示txt为空白谢谢谢谢谢谢。。。我将读取移动到单击..问题已解决
public void onClick(View arg0) {

            et=(EditText)findViewById(R.id.text);
            txt=et.getText().toString();

            new Read().execute("displayprice");

        }

    });