我有一点问题,我有一个AsyncTask,如果用户是否注册,我会从服务器得到响应,在调用之后,如果用户访问存在于另一个片段中,还存储在首选项中用户电子邮件,但是当我拨打getFragmentManager()
时遇到问题,有人可以帮助我吗?谢谢。这是我的代码。
LoginTask.java
public class LoginTask extends AsyncTask<String, Void, User> {
public LoginTask() {
}
@Override
protected User doInBackground(String... params) {
User user = new User();
user.setEmail(params[1]);
user.setPassword(params[2]);
// Set the Content-Type header
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(new MediaType("application", "json"));
HttpEntity<User> requestEntity = new HttpEntity<User>(user, requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the Jackson and String message converters
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
// Make the HTTP POST request, marshaling the request to JSON, and the response to a String
ResponseEntity<User> responseEntity = restTemplate.exchange(params[0], HttpMethod.POST, requestEntity, User.class);
return responseEntity.getBody();
}
@Override
protected void onPostExecute(User user) {
if (user.getSuccess()){
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, new AreaRiservataFragment()).commit();
}else{
}
}
}
我收到错误:Cannot Resolve Method 'getFragmentManager()'
。
答案 0 :(得分:0)
最佳解决方案可能是让您的AsyncTask
成为您活动的嵌套类。
public class YourActivity extends Activity {
...
class LoginTask extends AsyncTask<String, Void, User> {
...
@Override
protected User doInBackground(String... params) {
...
}
@Override
protected void onPostExecute(User user) {
...
}
}
}
如果您不可以,则可以将活动作为参数传递给AsyncTask
。看看好的例子Access instance variables from an activity inside a AsyncTask。