我有一些奇怪的問題。 Intent在共享首選項代碼之後不起作用。如果我寫SAME意向共享首選項代碼,它正在工作。下面是我的代碼...奇怪:意圖不工作後共享首選項代碼
檢查fetchData()方法...
Login_Activity
public class Login_Activity extends AppCompatActivity implements View.OnClickListener {
// Widgets
private Spinner spnr_login_type;
private EditText edt_login_id, edt_pwd;
private Button btn_login, btn_regi;
private ProgressDialog progressDialog;
// Variables
private String url = "login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Checking User Already Login OR Not
if (Preferences.getPreferences(getApplicationContext()).contains(Preferences.KEY_USER_NAME)) {
Intent myIntent = new Intent(Login_Activity.this, Home_Activity.class);
startActivity(myIntent);
finish();
}
find_view_by_id();
registerClickListeners();
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(true);
}
private void find_view_by_id() {
spnr_login_type = (Spinner) findViewById(R.id.spnr_login_type);
edt_login_id = (EditText) findViewById(R.id.edt_login_id);
edt_pwd = (EditText) findViewById(R.id.edt_pwd);
btn_login = (Button) findViewById(R.id.btn_login);
btn_regi = (Button) findViewById(R.id.btn_regi);
}
private void registerClickListeners() {
btn_login.setOnClickListener(this);
btn_regi.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_login:
if (Commom_Methods.isNetworkAvailable(getApplicationContext())) {
fetchData();
} else {
Toast.makeText(Login_Activity.this, "Please, Coonect to internet!!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_regi:
Intent myIntent = new Intent(Login_Activity.this, Regi_Activity.class);
startActivity(myIntent);
break;
}
}
private void fetchData() {
StringRequest stringReq = new StringRequest(Request.Method.POST, Constants.base_url + url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Resqq:", response);
progressDialog.dismiss();
if (response != null) {
try {
JSONObject jo = new JSONObject(response);
if (jo.has("success")) {
JSONObject joObj = jo.getJSONObject("user");
// Storing To Shared Preferences
Preferences.writeString(getApplicationContext(),
Preferences.KEY_SL_ID, joObj.getString("sl_id"));
Preferences.writeString(getApplicationContext(),
Preferences.KEY_TUTION_CENTER_SL, joObj.getString("tution_center_sl"));
Preferences.writeString(getApplicationContext(),
Preferences.KEY_BATCH_SL, joObj.getString("batch_sl"));
Preferences.writeString(getApplicationContext(),
Preferences.KEY_BATCH_GRUP_SL, joObj.getString("batch_grup_sl"));
Preferences.writeString(getApplicationContext(),
Preferences.KEY_CO_SL, joObj.getString("co_sl"));
Preferences.writeString(getApplicationContext(),
Preferences.KEY_DRANGE_SL, joObj.getString("drange_sl"));
Preferences.writeString(getApplicationContext(),
Preferences.KEY_REG_NO, joObj.getString("reg_no"));
Preferences.writeString(getApplicationContext(),
Preferences.KEY_USER_NAME, joObj.getString("name"));
Preferences.writeString(getApplicationContext(),
Preferences.KEY_USER_EMAIL, joObj.getString("email"));
Preferences.writeString(getApplicationContext(),
Preferences.KEY_USER_MOB, joObj.getString("mobile_no"));
Intent myIntent = new Intent(Login_Activity.this, Home_Activity.class);
startActivity(myIntent);
finish();
} else {
Toast.makeText(getApplicationContext(), jo.getString("error_msg"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(Login_Activity.this, "Server error! Server sent empty data!! Try again later.", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Error:" + error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("type", spnr_login_type.getSelectedItem().toString().toLowerCase());
params.put("reg_no", edt_login_id.getText().toString());
params.put("password", edt_pwd.getText().toString());
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(stringReq);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Fetching your details!");
progressDialog.show();
//progressDialog.show(new ContextThemeWrapper(this, R.style.DialogCustom), "Fetching your details!", "");
}
}
下面是我的反應......
{
"success": true,
"user": {
"sl_id": "1",
"reg_no": "21711101",
"name": "Akash Bharadwaj N",
"photo": "",
"email": "",
"type": "student"
},
"api_key": "APIkey Here"
}
define * Intent在共享首選項代碼* – Selvin
代碼太多後無法工作。不清楚,在哪一行不起作用。 –
您的logcat是否打印過任何JSON異常?考慮在這裏添加你的日誌 –