E/Volley: [925] NetworkDispatcher.processRequest: 未处理的异常 java.lang.RuntimeException: 错误 URL null
E/Volley: [925] NetworkDispatcher.processRequest: Unhandled exception java.lang.RuntimeException: Bad URL null
我启动了 Android 应用程序并且 运行 ( Main Activity )
,我之前用的数据列表是从网上展示的(也是加的),当我点了下activity(戳Activity),我在 LogCat 中收到错误 "java.net.MalformedURLException: Attempt to invoke virtual method 'int java.lang.String.length()' on a空对象引用
里面的代码戳Activity
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);
}
}```
Bad URL null 表示 url 错误或变量 url 没有任何值(表示空字符串)。您将从之前的 Activity 中获得 url。通过Logcat检查url是否正确。
这是一个错误,因为 re-initialising url 在 class 中再次出现 String url
只需删除 String 即可清除错误。
导致错误的代码:
String url = getIntent().getStringExtra("url");
正确代码:
url = getIntent().getStringExtra("url");
由于 url 已经定义并且它是从另一个 class.
传递过来的
我启动了 Android 应用程序并且 运行 ( Main Activity ) ,我之前用的数据列表是从网上展示的(也是加的),当我点了下activity(戳Activity),我在 LogCat 中收到错误 "java.net.MalformedURLException: Attempt to invoke virtual method 'int java.lang.String.length()' on a空对象引用
里面的代码戳Activity
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);
}
}```
Bad URL null 表示 url 错误或变量 url 没有任何值(表示空字符串)。您将从之前的 Activity 中获得 url。通过Logcat检查url是否正确。
这是一个错误,因为 re-initialising url 在 class 中再次出现 String url 只需删除 String 即可清除错误。
导致错误的代码:
String url = getIntent().getStringExtra("url");
正确代码:
url = getIntent().getStringExtra("url");
由于 url 已经定义并且它是从另一个 class.