【问题标题】:E/Volley: [925] NetworkDispatcher.processRequest: Unhandled exception java.lang.RuntimeException: Bad URL nullE/Volley:[925] NetworkDispatcher.processRequest:未处理的异常java.lang.RuntimeException:错误的URL null
【发布时间】:2020-08-31 07:43:02
【问题描述】:

我的 Android 应用已启动并运行(Main Activity) ,我使用了从互联网上显示的数据列表(也添加了),当我单击它进行下一个活动(PokeActivity)时,LogCat 中出现错误> as “java.net.MalformedURLException:尝试在空对象引用上调用虚拟方法'int java.lang.String.length()'”

PokeActivity里面的代码


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PokemonActivity extends AppCompatActivity {
    private TextView nameTextView;
    private TextView numberTextView;
    private TextView type1TextView;
    private TextView type2TextView;
    private String url;
    private RequestQueue requestQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pokemon);

        requestQueue= Volley.newRequestQueue(getApplicationContext());
        String url = getIntent().getStringExtra("url");
        nameTextView = findViewById(R.id.pokemon_name);
        numberTextView= findViewById(R.id.pokemon_number);
        type1TextView=findViewById(R.id.pokemon_type1);
        type2TextView=findViewById(R.id.pokemon_type2);
        load();

    }
    public void load (){
        type1TextView.setText("");
        type2TextView.setText("");
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    nameTextView.setText(response.getString("name"));
                    nameTextView.setText(String.format("#%3d",response.getInt("id")));
                    JSONArray typeEntries = response.getJSONArray("types");
                    for(int i = 0 ;i < typeEntries.length();i++){
                        JSONObject typeEntry = typeEntries.getJSONObject(i);
                        int slot = typeEntry.getInt("slot");
                        String type = typeEntry.getJSONObject("type").getString("name");
                        if(slot==1){
                            type1TextView.setText(type);
                        }else if(slot==2){
                            type2TextView.setText(type);
                        }
                    }
                } catch (JSONException e) {
                    Log.e("cs50", "Pokemon Json error", e);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("cs50","pokemon details error", error);
            }
        });
    requestQueue.add(request);
    }
}```

【问题讨论】:

    标签: android json nullpointerexception cs50


    【解决方案1】:

    Bad URL null 表示 url 错误或变量 url 没有任何值(表示空字符串)。您正在从以前的活动中获取 url。通过Logcat检查url是否正确。

    【讨论】:

    • 感谢回复,我犯的错误是在 onCreate 方法中再次重新初始化 url
    【解决方案2】:

    这是一个错误,因为在类中再次将 url 重新初始化为 String url 只需删除 String 即可清除错误。

    导致错误的代码: String url = getIntent().getStringExtra("url");

    正确代码: url = getIntent().getStringExtra("url"); 由于 url 已经定义并且它是从另一个类传递的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 2021-10-16
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多