在发布用gcm regID填充的字符串后,我在logcat中的null引用上获得了Volley addId.add(request),请问这里有什么问题? 也是boolean我发布上面的真实?我的意思是getRegId();只会在第一次运行时起作用吗?
public class MainActivity extends AppCompatActivity {
String projectNumber = "*********";
GoogleCloudMessaging gcm;
String regId ="";
String TAG = "Register Service";
RequestQueue addId;
String url = "***************";
String version = BuildConfig.VERSION_NAME;
boolean firstRun = true;
double d = Double.parseDouble(version);
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isNetworkConnected();
SharedPreferences prefs = getSharedPreferences("PREFS_NAME", 0);
firstRun = prefs.getBoolean("FIRST_RUN", true);
if(firstRun){
prefs = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("FIRST_RUN",false);
editor.apply();
getRegId();
}
...........
public void sendId() {
StringRequest request = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("The registration", "success sent");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("The registration", "error sent");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("gcmregid", regId);
return params;
}
};
addId.add(request);
}
public void getRegId() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
regId = instanceID.getToken(projectNumber, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
msg = "Device registration, registration ID=" + regId;
Log.i(TAG, msg);
if (regId.isEmpty()){
instanceID = InstanceID.getInstance(getApplicationContext());
regId = instanceID.getToken(projectNumber, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
}
}
} catch (IOException e) {
e.printStackTrace();
msg = "Error :" + e.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String s) {
sendId();
}
}.execute(null, null, null);
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
Toast.makeText(getApplicationContext(), "No Network", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
}
=============================================== ====================
答案 0 :(得分:0)
您没有在任何地方初始化addId
对象。你不应该RequestQueue addId = new RequestQueue();
而不仅仅是RequestQueue addId;
吗?
答案 1 :(得分:0)
添加此行,
-webkit-transform-origin: top left;
float:left;
在此行之前
{{1}}
您未初始化RequestQueue对象。