尝试使用Volley时我的应用程序崩溃

时间:2019-10-26 16:42:29

标签: java android android-volley

我尝试使用齐射捕获数据,但是我的应用程序崩溃了。该应用程序打开,但是当我按下搜索按钮时,我的应用程序崩溃了。

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);

}

我的应用程序在此处崩溃

      tvYear.setText(result);

当我删除此行时,我的应用程序不会崩溃。 我想查看requestin的值,但是它崩溃了

2 个答案:

答案 0 :(得分:0)

您可以在每行之后记录日志,以查看变量的值。 请指出究竟哪个日志猫显示错误?

答案 1 :(得分:0)

尝试改用JsonObjectRequest

您不必做可能导致转换的错误。如果在转换过程中出现任何问题并且对象映射不正确,那么您会遇到要解决的问题。

https://developer.android.com/training/volley/request