首页 > 解决方案 > My application crashes while trying to use Volley

问题描述

I try to capture data using a volley, but my application crashes. the application opens but my application crashes when I press the search button.

public class MainActivity extends AppCompatActivity {

TextView tvYear,tvDirector,tvActor,tvLanguage,tvPlots,tvCountry;
ImageView ivPoster;
EditText edName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edName     = findViewById(R.id.edName);
    tvYear     = findViewById(R.id.tvYear);
}

public void search(View view) {
    String mName = edName.getText().toString();
    if(mName.isEmpty())
    {
        edName.setError("please provide movie name");
        return;
    }
    String url = "HTTP://www.omdbapi.com/?t="+mName+"&plot=full&apikey=myApiKey";

    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest request = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject movie = new JSONObject(response);

                        String result = movie.getString("Response");
right there  crash app---->     tvYear.setText(result);
                        if(result.equals("True"))
                        {

                            Toast.makeText(MainActivity.this, "Found", Toast.LENGTH_SHORT).show();


                            if(posterUrl.equals("N/A"))
                            {

                            }
                            else
                            {

                            }
                        }
                        else
                        {

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );
    queue.add(request);

}

my application crashes right here

      tvYear.setText(result);

my application doesn't crash when I delete this line. I want to see the value of requestin but it crashes

标签: javaandroidandroid-volley

解决方案


您可以在每一行之后打印日志以查看变量的值。请指出究竟是什么日志猫显示错误?


推荐阅读