为什么我设置Icao = WSSS但是OnclickListener调用Icao = null不要使用= WSSS。
请给我指导。
public class MainActivity extends Activity {
String ans[] ={"","",""};
String Icao ="";
String url = String.format("http://flightxml.flightaware.com/json/FlightXML2/Search?query=-destination%%20%s",Icao);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Icao = "WSSS"; // <<< don't use this value
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getInput(url);
txt1.setText(ans[0]);
} });
}
答案 0 :(得分:0)
制作全局变量
String url;
现在在set
之后的onCreate()方法上Icao = "WSSS";
url = String.format("http://flightxml.flightaware.com/json/FlightXML2/Search?query=-destination%%20%s",Icao);
您的完整代码。
public class MainActivity extends Activity {
String ans[] ={"","",""};
String Icao ="";
String url ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Icao = "WSSS";
url = String.format("http://flightxml.flightaware.com/json/FlightXML2/Search?query=-destination%%20%s",Icao);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getInput(url);
txt1.setText(ans[0]);
} });
}
您的代码中的位置
Icao 字符串为空,您最初已经传递给url。显然,您总是会得到null。所以根据我的回答改变。