0
我只是在做一些學習,不明白爲什麼這個ArrayList被初始化,但不能在其填充的方法之外使用。誰能解釋一下?在此先感謝...ArrayList未初始化
例如,如果我在方法內部聲明ArrayList.size(),它表明它已被初始化,但在方法外的其他地方它不會。
package com.purewowstudio.animations;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
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.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.GsonBuilder;
import com.purewowstudio.animations.extras.CustomGrid;
import com.purewowstudio.animations.models.ActorArray;
import com.purewowstudio.animations.models.ActorObject;
import java.util.ArrayList;
public class ActorList extends ActionBarActivity {
String LOG_TAG = ActorList.class.getSimpleName();
private ArrayList<ActorObject> actorObjectArrayList;
ActorArray actorArray;
GridView actorGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actors_list);
getActors();
if (actorObjectArrayList != null) {
ActorObject[] actors = actorObjectArrayList.toArray(new ActorObject[actorObjectArrayList.size()]);
String actorsNames[] = new String[actorObjectArrayList.size()];
String actorsImages[] = new String[actorObjectArrayList.size()];
for (int i = 0; i < actorObjectArrayList.size(); i++) {
actorsNames[i] = actors[i].getName();
actorsImages[i] = actors[i].getImage();
}
CustomGrid adapter = new CustomGrid(ActorList.this, actorsNames, actorsImages);
actorGrid = (GridView) findViewById(R.id.grid);
actorGrid.setAdapter(adapter);
actorGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Toast.makeText(ActorList.this, "You Clicked at " + actorsNames[+position], Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.animation_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void getActors(){
String url ="http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors";
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
actorArray = new GsonBuilder().create().fromJson(response, ActorArray.class);
actorObjectArrayList = actorArray.getActors();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.v(LOG_TAG, "Volley Error");
}
});
queue.add(stringRequest);
}
}
如果它是本地方法的本地方法,那就是Java的工作方式(大多數語言,我會說)。 –
你的'getActors'方法是異步的。當你調用'actorObjectArrayList!= null'時,它還沒有被初始化,因爲'public void onResponse(String response)'將來會被調用。 – njzk2
我認爲可能是這種情況,所以我把onCreate中的所有東西都加入到它自己的方法中,然後我在10秒內要求該方法啓動,但仍然無法工作?什麼是排球時間喜歡與Volley(我第一次使用它)。我知道服務器響應會有延遲 –