嘿伙计们我试图以json格式获取数据,而不是在旋转器中显示它们。 我陷入了一个问题,你可以帮助我。 这是我的代码。
public class MainActivity extends Activity {
Spinner selectcityspinner, selectareaspinner;
ArrayList<city> citylist;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectcityspinner = (Spinner)findViewById(R.id.selectcityspin);
selectareaspinner = (Spinner) findViewById(R.id.selectareaspin);
List<city> citylist = new ArrayList<>();
new cityAsynTask().execute("http://bund.webs.com/citylist.txt");
/*ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_item,citylist);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectcityspinner.setAdapter(adapter);
selectcityspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position) + " selected",Toast.LENGTH_LONG).show();
String Country = String.valueOf(parent.getItemAtPosition(position));
if (Country.contentEquals("Ahmedabad"))
{
List<String> AreaList = new ArrayList<String>();
AreaList.add("Ghatlodia");
AreaList.add("Chandlodia");
AreaList.add("Others");
ArrayAdapter<String> adapter1 = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_item,AreaList);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter1.notifyDataSetChanged();
selectareaspinner.setAdapter(adapter1);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});*/
}
public class cityAsynTask extends AsyncTask<String,Void,Boolean>
{
@Override
protected Boolean doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jobjcity = new JSONObject(data);
JSONArray jarray = jobjcity.getJSONArray("actors");
for (int i = 0;i < jarray.length();i++){
city city = new city();
JSONObject jrealobject = jarray.getJSONObject(i);
city.setCity(jrealobject.getString("name"));
citylist.add(city);
}
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if(aBoolean == false){
Toast.makeText(MainActivity.this,"data isnot recieved",Toast.LENGTH_SHORT).show();
}
}
}
}
city.java
public class city {
private String city;
public city(String city) {
this.city = city;
}
public city() {
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
这是我的错误
02-28 02:12:44.044 2863-2880/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.bhaumik.myparsesignup, PID: 2863
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.bhaumik.myparsesignup.MainActivity$cityAsynTask.doInBackground(MainActivity.java:115)
at com.example.bhaumik.myparsesignup.MainActivity$cityAsynTask.doInBackground(MainActivity.java:95)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
你能帮助我吗????
真的很感激。
答案 0 :(得分:2)
替换:
List<city> citylist = new ArrayList<>();
使用:
citylist = new ArrayList<>();
这样,您可以正确初始化citylist
字段,而不是定义新的(未使用的)本地变量,因此以后不会null
。