gpt4 book ai didi

java - 致命异常 : AsyncTask #1 how fix?

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:13 27 4
gpt4 key购买 nike

抱歉,我刚刚学习使用 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);
}

}

我的日志猫..

    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)

请帮助我,请指导我......

最佳答案

如果是第31行

if (!(boolean)hasil.get("sukses")){

因此,您在标签上犯了一个拼写错误,get() 返回 null 并将其转换为 boolean 值会导致 NPE。

关于java - 致命异常 : AsyncTask #1 how fix?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42875564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com