[英]intent.putExtra returns correct value in fragment but returns null in activity
我需要從MainActivity獲取account
以添加到AddModifyTask中的命令。 我在MainActivity中使用intent.putExtra
並在AddModifyTask中通過getIntent.getStringExtra
獲取account
,但它始終返回null
,與Fragment中的getIntent.getStringExtra
仍然相同,然后它返回正確的account
值,誰能幫助我
MainAcitivy,我需要從這里傳遞account
Boolean result = db.checkUserNamePassword(account, password);
if(result==true) {
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("account", account);
Intent intent1 = new Intent(MainActivity.this, AddModifyTask.class);
intent1.putExtra("account", account);
startActivity(intent);
我需要將account
傳遞到的 AddModifyTask 活動,但它只返回null
public void saveTask(View v) {
/*Checking for Empty Task*/
if (edit_text.getText().toString().trim().length() > 0) {
if (isModify) {
String account = getIntent().getStringExtra("account");
mydb.updateTask(task_id, edit_text.getText().toString(), new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()), account);
Toast.makeText(getApplicationContext(), "Task updated.", Toast.LENGTH_SHORT).show();
} else {
String account = getIntent().getStringExtra("account");
mydb.insertTask(edit_text.getText().toString(), new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()), account);
Toast.makeText(getApplicationContext(), "Added.", Toast.LENGTH_SHORT).show();
}
finish();
} else {
Toast.makeText(getApplicationContext(), "No empty.", Toast.LENGTH_SHORT).show();
}
仍然與Fragment中的getIntent.getStringExtra
相同,然后它返回正確的account
值
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_profile, container, false);
TextView textViewAccount = v.findViewById(R.id.textViewAccount);
String account = getActivity().getIntent().getStringExtra("account");
textViewAccount.setText(account);
}
return v;
}
數據庫中的函數insertTask
和updateTask
,是數據庫中的語句還是AddModifyTask中的getIntent
或兩者都有問題?
public boolean insertTask(String task, String task_at, String account) {
Date date;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("task", task);
contentValues.put("task_at", task_at);
contentValues.put("status", 0);
contentValues.put("user", account);
db.insert(TB_TASK, null, contentValues);
return true;
}
public boolean updateTask(String id, String task, String task_at, String account) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("task", task);
contentValues.put("task_at", task_at);
contentValues.put("user", account);
db.update(TB_TASK, contentValues, "id = ? ", new String[]{id});
return true;
}
請幫忙!
可能,您的問題在這里:
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("account", account);
Intent intent1 = new Intent(MainActivity.this, AddModifyTask.class);
intent1.putExtra("account", account);
startActivity(intent);
您正在為AddModifyTask
創建intent1
,但正在為HomeActivity
創建intent
。
嘗試類似:
Intent intent = new Intent(MainActivity.this, AddModifyTask.class);
intent.putExtra("account", account);
startActivity(intent);
我使用SharedPreferences
而不是intent.putExtra
Boolean result = db.checkUserNamePassword(account, password);
if(result==true) {
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("account", account);
SharedPreferences sharedPref = getSharedPreferences("account", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("account", account);
editor.apply();
startActivity(intent);
在 AddModifyTask
public void saveTask(View v) {
/*Checking for Empty Task*/
if (edit_text.getText().toString().trim().length() > 0) {
if (isModify) {
SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
String account = sharedPreferences.getString("account","");
mydb.updateTask(task_id, edit_text.getText().toString(), new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()), account);
Toast.makeText(getApplicationContext(), "Tác vụ đã được cập nhật.", Toast.LENGTH_SHORT).show();
} else {
SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
String account = sharedPreferences.getString("account","");
mydb.insertTask(edit_text.getText().toString(), new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()), account);
Toast.makeText(getApplicationContext(), "Đã thêm.", Toast.LENGTH_SHORT).show();
}
finish();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.