您好我已经搜索了Stackoverflow和Google,我相信我的错误是我没有发起变量电子邮件。但我无法确定我的错误在我的代码中的位置。任何帮助,将不胜感激。感谢。
我得到的错误是。
FATAL EXCEPTION:主进程:com.xxx.loginandregistration,PID:5821 java.lang.NullPointerException:尝试调用虚方法 'java.lang.String com.xxx.loginandregistration.User.getEmail()'上的一个 null对象引用 com.xxx.loginandregistration.LoginFragment $ 1.onResponse(LoginFragment.java:105) 在 retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.jav ...
我认为我的错误在于我的loginProcess方法,但我不确定我做错了什么。
private void loginProcess(String email,String password){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
User user = new User();
user.setEmail(email);
user.setPassword(password);
ServerRequest request = new ServerRequest();
request.setOperation(Constants.LOGIN_OPERATION);
request.setUser(user);
Call<ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
if(resp.getResult().equals(Constants.SUCCESS)){
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN,true);
editor.putString(Constants.EMAIL,resp.getUser().getEmail());
editor.putString(Constants.FIRSTNAME,resp.getUser().getFirstname());
editor.putString(Constants.LASTNAME,resp.getUser().getLastname());
editor.putString(Constants.UNIQUE_ID,resp.getUser().getUnique_id());
editor.apply();
goToProfile();
}
progress.setVisibility(View.INVISIBLE);
}
我的完整代码
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by hozda_000 on 4/19/2016.
*/
public class LoginFragment extends Fragment implements View.OnClickListener {
private AppCompatButton loginButton;
private EditText emailEditText;
private EditText passwordEditText;
private TextView registerTextView;
private ProgressBar progress;
private SharedPreferences pref;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login, container,false);
initViews(view);
return view;
}
private void initViews(View view) {
pref = getActivity().getPreferences(0);
loginButton = (AppCompatButton)view.findViewById(R.id.loginButton);
registerTextView = (TextView)view.findViewById(R.id.registerTextView);
emailEditText = (EditText)view.findViewById(R.id.emailEditText);
passwordEditText = (EditText)view.findViewById(R.id.passwordEditText);
progress = (ProgressBar)view.findViewById(R.id.progress);
loginButton.setOnClickListener(this);
registerTextView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.registerTextView:
goToRegister();
break;
case R.id.loginButton:
String email = emailEditText.getText().toString();
String password = passwordEditText.getText().toString();
if(!email.isEmpty() && !password.isEmpty()) {
progress.setVisibility(View.VISIBLE);
loginProcess(email, password);
} else {
Snackbar.make(getView(), "You have empty fields!", Snackbar.LENGTH_LONG).show();
}
break;
}
}
private void loginProcess(String email,String password){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
User user = new User();
user.setEmail(email);
user.setPassword(password);
ServerRequest request = new ServerRequest();
request.setOperation(Constants.LOGIN_OPERATION);
request.setUser(user);
Call<ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
if(resp.getResult().equals(Constants.SUCCESS)){
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN,true);
editor.putString(Constants.EMAIL,resp.getUser().getEmail());
editor.putString(Constants.FIRSTNAME,resp.getUser().getFirstname());
editor.putString(Constants.LASTNAME,resp.getUser().getLastname());
editor.putString(Constants.UNIQUE_ID,resp.getUser().getUnique_id());
editor.apply();
goToProfile();
}
progress.setVisibility(View.INVISIBLE);
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
progress.setVisibility(View.INVISIBLE);
Log.d(Constants.TAG, "failed");
Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
}
});
}
private void goToRegister(){
Fragment register = new RegisterFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, register);
ft.commit();
}
private void goToProfile(){
Fragment profile = new ProfileFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, profile);
ft.commit();
}
}
答案 0 :(得分:0)
editor.putString(Constants.EMAIL,resp.getUser().getEmail());
此行导致错误。
resp.getUser()
返回的对象为null
,因此在null对象上调用getEmail()
方法会为您提供NullPointerException