我在android编程时遇到问题,我有一个需要运行一些例程(例如扫描某些条形码)的应用.
扫描完条形码后,我会将条形码的数据发送到我的API,“等待”以获取一些信息,然后做其他事情.
问题是当我调用我的API来获取信息,并尝试使用应该来自API的信息后,我陷入了NulL异常,请参见以下代码:
每次扫描条形码时都会调用的扫描例程:
// 1 - First Stage: Scan barcode of tank if(stageScannerTank == 1) { // Verify if the user scanned the correct Tank if (!resultScan.equals(expectedTank)) { Toast.makeText(getContext(), "Wrong Tank, scan the correct ", Toast.LENGTH_LONG).show(); stageScannerTank = 0; return; } // Get the tank information //new TanqueTask().execute(resultScan); **HERE IS MY PROBLEM, When I call the function below, I fill my tankMixPlant with data** getTankMixPlant(resultScan); ** I always fall into this IF ** // If null information if(tankMixPlant == null) { Toast.makeText(getContext(), "Error to read tank information", Toast.LENGTH_LONG).show(); return; } }
getTankMixPlant,使用Retrofit来调用我的服务,如果我超出常规测试,它就可以正常工作:
private voID getTankMixPlant(String tankname){ mService.getTankMixPlant(tankname).enqueue(new Callback<TankMixPlant>() { @OverrIDe public voID onResponse(Call<TankMixPlant> call, Response<TankMixPlant> response) { tankMixPlant = response.body(); } @OverrIDe public voID onFailure(Call<TankMixPlant> call, Throwable t) { // On failure show the error message to the User AlertDialog alert; AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Error"); builder.setMessage("Error: " + t.getMessage()); alert = builder.create(); alert.show(); } });}
如何等待tankMixPlant充满数据以继续执行我的例程?
更新:
我继续解决该问题,但是我更改了一些可以在下面看到的内容:
现在我得到了NullPointerException.
现在,我验证onResponse方法内的对象是否为null:
private voID getTankMixPlant(String tankname){ mService.getTankMixPlant(tankname).enqueue(new Callback<TankMixPlant>() { @OverrIDe public voID onResponse(Call<TankMixPlant> call, Response<TankMixPlant> response) { tankMixPlant = response.body(); if(tankMixPlant == null) { Toast.makeText(getContext(), "Error to read tank information", Toast.LENGTH_LONG).show(); return; } } @OverrIDe public voID onFailure(Call<TankMixPlant> call, Throwable t) { // On failure show the error message to the User AlertDialog alert; AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Error"); builder.setMessage("Error: " + t.getMessage()); alert = builder.create(); alert.show(); } });}
但是,当我尝试验证有关tankMixPlant的一些信息时:
// Verify tank stage if(!tankMixPlant.getBlenderCode().equals("0") && tankMixPlant.getIngr() == 1) { getPackages(tankMixPlant.getRecipeNumber(), tankMixPlant.getBlenderCode(), tankMixPlant.getTankname()); // 2 - Mix Packages: Scan package barcode stageScannerTank = 2; } if(!tankMixPlant.getIngrSAPNumber().equals("0") && tankMixPlant.getIngr() == 1) { getIngr(tankMixPlant.getRecipeNumber(), tankMixPlant.getTankname()); // 3 - P.I Packages: Scan package barcode stageScannerTank = 3; }
我的应用因以下错误而崩溃:
E/AndroIDRuntime: FATAL EXCEPTION: main Process: com.example.labtse.kibonapp, PID: 21519 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=114910, result=-1, data=Intent { act=com.Google.zxing.clIEnt.androID.SCAN flg=0x80000 VirtualScreenParam=Params{mdisplayID=-1, null, mFlags=0x00000000)} (has extras) }} to activity {com.example.labtse.kibonapp/com.example.labtse.kibonapp.TabPagesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.labtse.kibonapp.model.TankMixPlant.getBlenderCode()' on a null object reference at androID.app.ActivityThread.deliverResults(ActivityThread.java:5007) at androID.app.ActivityThread.handleSendResult(ActivityThread.java:5050) at androID.app.ActivityThread.access00(ActivityThread.java:230) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1876) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:148) at androID.app.ActivityThread.main(ActivityThread.java:7409) at java.lang.reflect.Method.invoke(Native Method) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.labtse.kibonapp.model.TankMixPlant.getBlenderCode()' on a null object reference at com.example.labtse.kibonapp.MixPlantFragment.scanRoutine(MixPlantFragment.java:367) at com.example.labtse.kibonapp.MixPlantFragment.onActivityResult(MixPlantFragment.java:399) at androID.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151) at androID.app.Activity.dispatchActivityResult(Activity.java:7165) at androID.app.ActivityThread.deliverResults(ActivityThread.java:5003) at androID.app.ActivityThread.handleSendResult(ActivityThread.java:5050) at androID.app.ActivityThread.access00(ActivityThread.java:230) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1876) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:148) at androID.app.ActivityThread.main(ActivityThread.java:7409) at java.lang.reflect.Method.invoke(Native Method) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我的tankMixPlant类:
package com.example.labtse.kibonapp.model;public class TankMixPlant { private String tankname; private String recipeNumber; private int IDBatch; private String ingrSAPNumber; private String blenderCode; private short ingr; private boolean QRUN; private boolean QEND; private Boolean QDOSANDO; private Boolean CONF_ING; public TankMixPlant() { } public String getTankname() { return tankname; } public voID setTankname(String tankname) { this.tankname = tankname; } public String getRecipeNumber() { return recipeNumber; } public voID setRecipeNumber(String recipeNumber) { this.recipeNumber = recipeNumber; } public int getIDBatch() { return IDBatch; } public voID setIDBatch(int IDBatch) { this.IDBatch = IDBatch; } public String getIngrSAPNumber() { return ingrSAPNumber; } public voID setIngrSAPNumber(String ingrSAPNumber) { this.ingrSAPNumber = ingrSAPNumber; } public String getBlenderCode() { return blenderCode; } public voID setBlenderCode(String blenderCode) { this.blenderCode = blenderCode; } public short getIngr() { return ingr; } public voID setIngr(short ingr) { this.ingr = ingr; } public boolean isQRUN() { return QRUN; } public voID setQRUN(boolean QRUN) { this.QRUN = QRUN; } public boolean isQEND() { return QEND; } public voID setQEND(boolean QEND) { this.QEND = QEND; } public Boolean getQDOSANDO() { return QDOSANDO; } public voID setQDOSANDO(Boolean QDOSANDO) { this.QDOSANDO = QDOSANDO; } public Boolean getCONF_ING() { return CONF_ING; } public voID setCONF_ING(Boolean CONF_ING) { this.CONF_ING = CONF_ING; }
}
解决方法:
解:
您必须这样写:
if(tankMixPlant == null) { Toast.makeText(getContext(), "Error to read tank information", Toast.LENGTH_LONG).show(); return; }
在您的onResponse内部,行tankplant = …之后
现在,如果您从服务器获取任何数据,则将消除空异常.
希望能帮助到你.
更新1:
这样写:
if(!tankMixPlant.getBlenderCode().equals("0") && tankMixPlant.getIngr() == 1) { getPackages(tankMixPlant.getRecipeNumber(), tankMixPlant.getBlenderCode(), tankMixPlant.getTankname()); // 2 - Mix Packages: Scan package barcode stageScannerTank = 2; } if(!tankMixPlant.getIngrSAPNumber().equals("0") && tankMixPlant.getIngr() == 1) { getIngr(tankMixPlant.getRecipeNumber(), tankMixPlant.getTankname()); // 3 - P.I Packages: Scan package barcode stageScannerTank = 3; }
在您的onResponse方法中,如下所示:
private voID getTankMixPlant(String tankname){mService.getTankMixPlant(tankname).enqueue(new Callback<TankMixPlant>() { @OverrIDe public voID onResponse(Call<TankMixPlant> call, Response<TankMixPlant> response) { tankMixPlant = response.body(); if(tankMixPlant == null) { Toast.makeText(getContext(), "Error to read tank information", Toast.LENGTH_LONG).show(); return; } ...... (Write Here) } @OverrIDe public voID onFailure(Call<TankMixPlant> call, Throwable t) { // On failure show the error message to the User AlertDialog alert; AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Error"); builder.setMessage("Error: " + t.getMessage()); alert = builder.create(); alert.show(); }});
试试吧.
总结以上是内存溢出为你收集整理的Android:等待API做出响应,然后再使用Retrofit继续执行全部内容,希望文章能够帮你解决Android:等待API做出响应,然后再使用Retrofit继续执行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)