抱歉,我只是学习使用AsyncTask,但运行代码时出现问题..
我有三节课 RestClient的第一个类public class LoginPembayaran {
private static final String urlServer = "https://webservicetagihan.herokuapp.com";
RestTemplate restTemplate = new RestTemplate();
public void login(String username, String password) throws GagalLoginException{
Map<String, String> logindata = new HashMap<>();
logindata.put("username",username);
logindata.put("password",password);
Map<String,Object> hasil = restTemplate.postForObject(urlServer + "/api/login",logindata,Map.class,new Object[]{});
if (hasil == null ){
throw new GagalLoginException("Respon valid");
}
if (!(Boolean)hasil.get("sukses")){
throw new GagalLoginException("user name / password salah");
}
}
AsyncTask的第二课..
public class loginAktifity extends Fragment {
private LoginPembayaran loginPembayaran = new LoginPembayaran();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View fragment = inflater.inflate(R.layout.fr_login,container,false);
Button button = (Button)fragment.findViewById(R.id.BtnLogin);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = ((EditText)fragment.findViewById(R.id.Etusernama)).getText().toString();
String password = ((EditText)fragment.findViewById(R.id.EtPassword)).getText().toString();
new AsyncTask<String,Void,Boolean>(){
String ErrorMassage;
@Override
protected Boolean doInBackground(String... params) {
try {
loginPembayaran.login(params[0], params[1]);
return true;
}catch (GagalLoginException err){
ErrorMassage = err.getMessage();
return false;
}
};
@Override
protected void onPostExecute(Boolean sukses) {
if(sukses){
Intent intent = new Intent(getContext(),SesudahLoginActivity.class);
startActivity(intent);
}
else {
Toast.makeText(getContext(),ErrorMassage,Toast.LENGTH_LONG).show();
}
}
}.execute(username,password);
}
}
);
return fragment;
}
}
异常的类树
public class GagalLoginException extends Exception {
public GagalLoginException(String message) {
super(message);
}
}
我的logcat ..
03-18 09:57:35.780 30964-31227/com.khoiron.apptagihan E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.khoiron.apptagihan, PID: 30964
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.khoiron.apptagihan.RestClient.LoginPembayaran.login(LoginPembayaran.java:31)
at com.khoiron.apptagihan.Fragment.loginAktifity$1$1.doInBackground(loginAktifity.java:48)
at com.khoiron.apptagihan.Fragment.loginAktifity$1$1.doInBackground(loginAktifity.java:41)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12q
at java.lang.Thread.run(Thread.java:841)
请帮帮我。请指导我......
答案 0 :(得分:0)
如果是第31行
if (!(boolean)hasil.get("sukses")){
所以你用标签做了拼写错误,get()返回null并将其强制转换为布尔值导致NPE。
答案 1 :(得分:0)
从异常日志中,您可以看到异常来自{31}的LoginPembayaran.login
。
Caused by: java.lang.NullPointerException
at com.khoiron.apptagihan.RestClient.LoginPembayaran.login(LoginPembayaran.java:31)
正如在其他答案中已经提到的,地图hasil
可能不包含关键项sukses
,因此例外。
要调试此问题,您可以使用Android Studio的调试器(请参阅official guide)并在第31行设置断点,以检查地图hasil
是否确实包含密钥{{ 1}}。