我正在使用导航抽屉活动,我正在加载一个listview片段,其中应该加载从服务器检索的一些文本。我正在使用volley发送JSON Object请求来检索数据。我正在使用回调接口实现来确保活动将等待响应。在我使用它的所有其他实例中,它运行得非常好。但是在这种情况下它不是,因为控件被转移回OnCreate方法而不等待回调实现方法的完成。我将在下面的代码中说明。
//import statements
public class HomeTypeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar = null;
NavigationView navigationView = null;
MenuItem item;
final ArrayList<String> updates_list = new ArrayList<String>();
final ArrayList<String> time_list = new ArrayList<String>();
private RequestQueue queue;
private final String server_url = "http://10.0.2.2";
private StringBuilder get_updates_time_url = new StringBuilder(server_url).append("get_updates.php");
final String TAG = "Loggin";
Bundle bundle = new Bundle();
Fragment Updates_Fragment = null;
Fragment Clients_Fragment = null;
Fragment Reminders_Fragment = null;
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_type);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fabAction();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
try {
Updates_Fragment = (Fragment) Updates.class.newInstance();
Reminders_Fragment = (Fragment) Upcoming_Reminders.class.newInstance();
Clients_Fragment = (Fragment) ClientList.class.newInstance();
//Updates_Fragment
} catch (Exception e) {
Log.i(TAG, e.toString());
}
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Class FragmentClass = Updates.class;
Fragment fragment = null;
try {
fragment = (Fragment) FragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commit();
navigationView.getMenu().getItem(0).setChecked(true);
setTitle(navigationView.getMenu().getItem(0).getTitle());
Bundle confirm = getIntent().getExtras();
if (confirm == null)
return;
else {
String message = confirm.getString("user_full_name");
String client_toast = confirm.getString("client_message");
if (message == null)
return;
if (toast_flag) {
Toast.makeText(this, "Welcome " + message + "!", Toast.LENGTH_SHORT).show();
toast_flag = false;
}
if (client_toast != null)
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
Populator();
try {
Log.i(TAG, bundle.getStringArrayList("updates_array").toString());
} //Catching a null pointer exception here as Populator method returns without having waited for the response
catch (NullPointerException exception) {
Log.i(TAG, exception.toString() + " null bundle why?");
}
Updates_Fragment.setArguments(bundle);
}
public interface UpdatesVolleyCallback{
public void onResponse(JSONObject response);
}
public void Populator() {
Log.i(TAG, "callback interface implementer");
timeVolleyRequest(Request.Method.POST, get_updates_time_url.toString(), new UpdatesVolleyCallback() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array = response.getJSONArray("updates");
//Log.i(TAG, array.toString());
for (int i = 0; i < array.length(); i++) {
String update = array.getJSONObject(i).getString("update_string");
//Log.i(TAG, update);
String time = array.getJSONObject(i).getString("update_time");
//Log.i(TAG, time);
updates_list.add(update);
time_list.add(time);
Log.i(TAG, updates_list.toString());
bundle.putStringArrayList("updates_array",updates_list);
}
} catch (JSONException e) {
}
}
});
}
public void timeVolleyRequest(int method, String url, final UpdatesVolleyCallback callback) {
Log.i(TAG, "I'm at volley!");
queue = MySingleton.getInstance(HomeTypeActivity.this.getApplicationContext()).getRequestQueue();
final ArrayList<String> updates_list = new ArrayList<String>();
final ArrayList<String> time_list = new ArrayList<String>();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(method, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, "Inside response!");
callback.onResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
AlertDialog.Builder builder = new AlertDialog.Builder(HomeTypeActivity.this);
builder.setMessage("Could not connect to the network. Try again later!")
.setTitle("Error!");
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).show();
}
});
queue.add(jsonObjectRequest);
}
}
以下是基于我的标签的日志内容
09-28 16:11:21.026 4013-4013/com.onerooftechnologiesamc I/Loggin: callback interface implementer yo
09-28 16:11:21.027 4013-4013/com.onerooftechnologiesamc I/Loggin: I'm at volley!
09-28 16:11:21.042 4013-4013/com.onerooftechnologiesamc I/Loggin: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.util.ArrayList.toString()' on a null object reference null bundle why?
09-28 16:11:21.793 4013-4013/com.onerooftechnologiesamc I/Loggin: Inside Response!
//And then here the response log tags are printed.
我已经调试了2天了。我尝试替换代码,我甚至复制粘贴我用于其他模块的代码的确切结构。我在这里错过了什么?我甚至尝试使用wait()和notifyAll(),但这只是将我的整个应用程序发送到永久阻塞状态。即使让OnCreate休眠5秒,也会在生成空指针异常并且一切都相同之前将整个应用程序发送到阻塞状态。感谢帮助!
PS:我没有复制粘贴活动的所有方法!