[英]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.