我正在將數據從一個活動發送到另一個活動。 但獲取錯誤空指針異常和應用程序崩潰。將數據從一個活動傳遞到另一個活動時出錯
錯誤 -
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.a98fit.neeraj.a98fit, PID: 31167
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
at com.a98fit.neeraj.a98fit.Signup$1.onClick(Signup.java:118)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Application terminated.
一個活動 - 我正在存儲數據令牌&用戶id在數據庫和經由意圖發送到另一個活動。
Intent intent1 = new Intent(Name.this, Signup.class);
intent1.putExtra("userId", userId);
intent1.putExtra("token",token);
startActivity(intent1);
另一項活動 - 在那裏我得到
Bundle bundle = getIntent().getExtras();
String token = bundle.getString("token").toString();
String userId= bundle.getString("userId");
一個活動---
public class Name extends AppCompatActivity {
private static final String TAG = Name.class.getSimpleName();
private Button btn_name_next;
EditText emailid,editTextUserName;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
btn_name_next = (Button) findViewById(R.id.btn_name_next);
editTextUserName=(EditText) findViewById(R.id.editTextUserName);
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
final String deviceId = deviceUuid.toString();
Log.e(TAG, "Device Id: " + deviceId);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// Session manager
session = new SessionManager(getApplicationContext());
// SQLite database handler
db = new SQLiteHandler(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
Intent intent = new Intent(Name.this,
MakemeHealthy.class);
startActivity(intent);
finish();
}
btn_name_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=editTextUserName.getText().toString();
if (name.length()==0) {
editTextUserName.setError("Tell us your name atleast");
}
else
{
//launchAgeScreen();
registerUser(name, deviceId);
}
}
});
}
private void registerUser(final String name, final String deviceId) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.NAME_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean success = jObj.getBoolean("success");
//boolean error = jObj.getBoolean("error");
if (success) {
// User successfully stored in MySQL
// Now store the user in sqlite
//String uid = jObj.getString("uid");
String userId =jObj.getString("userId");
String token = jObj.getString("token");
db.addUser(token,userId);
//db.addUser(userId);
Intent intent1 = new Intent(Name.this, Signup.class);
intent1.putExtra("userId", userId);
intent1.putExtra("token",token);
startActivity(intent1);
Log.e(TAG, "token: " + token);
Log.e(TAG, "userId: " + userId);
Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(
Name.this,
Signup.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
//params.put("email", email);
// params.put("password", password);
params.put("deviceId", deviceId);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
另一個活動 - 我在哪裏發送數據。
intent1.putExtra( 「令牌」,令牌);你的令牌是字符串值嗎? –
是的,它是字符串 – user6313669
請試試我的答案。希望它能幫助你。 –