问题是我无法从onCreate方法中访问userId变量值。我正在从两个不同的活动访问活动,所以我必须检查我来自哪一个,我用调用者来做,但getDetailsUserId方法返回的值一直是0,即使userId是一个类变量
如果我用5初始化userId,例如getDetailsUserId方法返回的值...而不是onCreate方法的值。
public class Details extends Activity {
long userId;
long getDetailsUserId(){
return userId; //This is the variable value I can't get.
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
getActionBar().setDisplayHomeAsUpEnabled(true);
final String caller;
//Get previous activity name
caller = getIntent().getStringExtra("com.mysite.myapp.Caller");
//Get user id from "List"
if(caller.equals("List")){
List userListId = new List();
userId = userListId.getUserListId();
}
//Get user id from "Profile Saved"
else if(caller.equals("ProfileSaved")){
ProfileSaved savedUserId = new ProfileSaved();
userId = savedUserId.getSavedUserId();
}
}
答案 0 :(得分:1)
抱歉,改变了我之前的回答。尝试过,它对我有用。
这是
public static class Details extends Activity {
static long userId;
long getDetailsUserId(){
return userId; //This is the variable value I can't get.
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
getActionBar().setDisplayHomeAsUpEnabled(true);
final String caller;
//Get previous activity name
caller = getIntent().getStringExtra("com.mysite.myapp.Caller");
//Get user id from "List"
if(caller.equals("List")){
List userListId = new List();
userId = userListId.getUserListId();
}
//Get user id from "Profile Saved"
else if(caller.equals("ProfileSaved")){
ProfileSaved savedUserId = new ProfileSaved();
this.userId = savedUserId.getSavedUserId();
}
}
}