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

标签 java android android-volley

我尝试使用 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 的值,但它崩溃了

最佳答案

您可以在每行之后打印日志以查看变量的值。 请指出log cat显示的错误到底是什么?

关于java - 我的应用程序在尝试使用 Volley 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58572720/

相关文章:

java - 左连接获取和预取

java - 上下文切换时间

android - fragment 在 fragmentActivity 之前运行

javascript - 自动验证 htaccess (401) 用户名​​密码登录

java - appendChild 处的 DOMException

java - 仅在成员函数内创建嵌套内部类的对象

Android Google Map 在 Fragment 中总是显示 SupportMapFragment 为 null

java - 如何在尝试执行注册 Activity 时修复 Android 中的 "com.android.Volley.TimeoutError"

Android Volley SQLite 集成/组合

java - 与 Volley 建立联系