嗨,我正在尝试使用Retrofit解析JSON,将其保存在Sqlite上并显示在RecyclerView上。但是,当我尝试打开活动时,我的应用程序崩溃了。 以下是我相关活动的完整代码。您能帮我解决问题吗? 谢谢
public class InventoryProductActivity extends AppCompatActivity implements InventoryProductListAdapter.CustomClickListener {
private static final String TAG = InventoryProductActivity.class.getSimpleName();
private InventoryProductListAdapter mInventoryProductListAdapter;
private RecyclerView mRecyclerView;
private RetrofitClient mRetrofitClient;
LinearLayoutManager mLinearLayoutManager;
private WarehouseDatabase mDatabase;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory_product);
configViews();
mRetrofitClient = new RetrofitClient();
mDatabase = new WarehouseDatabase(this);
loadInventoryProductFeed();
}
private void configViews() {
mRecyclerView = findViewById(R.id.recycler_view_inventory_product);
mRecyclerView.setHasFixedSize(true);
mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mInventoryProductListAdapter = new InventoryProductListAdapter(this);
mRecyclerView.setAdapter(mInventoryProductListAdapter);
}
private void loadInventoryProductFeed() {
mProgressDialog = new ProgressDialog(InventoryProductActivity.this);
mProgressDialog.setMessage("Loading Inventory Data...");
mProgressDialog.setCancelable(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setIndeterminate(true);
mProgressDialog.show();
mInventoryProductListAdapter.reset();
if (getNetworkAvailability()) {
getFeed();
} else {
getFeedFromDatabase();
}
}
private void getFeed() {
Call<List<InventoryProductModel>> listCall = mRetrofitClient.getWarehouseServiceInventoryProduct().getAllInventoryProducts();
listCall.enqueue(new Callback<List<InventoryProductModel>>() {
@Override
public void onResponse(Call<List<InventoryProductModel>> call, Response<List<InventoryProductModel>> response) {
if (response.isSuccessful()) {
List<InventoryProductModel> inventoryProductModelList = response.body();
for (int i = 0; i < inventoryProductModelList.size(); i++) {
InventoryProductModel inventoryProductModel = inventoryProductModelList.get(i);
mInventoryProductListAdapter.notifyDataSetChanged();
}
} else {
int sc = response.code();
switch (sc) {
}
}
mProgressDialog.dismiss();
}
@Override
public void onFailure(Call<List<InventoryProductModel>> call, Throwable t) {
mProgressDialog.dismiss();
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void getFeedFromDatabase() {
List<InventoryProductModel> inventoryProductModelList = mDatabase.getInventoryProducts();
for (int i = 0; i < inventoryProductModelList.size(); i++) {
InventoryProductModel inventoryProductModel = inventoryProductModelList.get(i);
Log.d(TAG, inventoryProductModel.getName() + "||" + inventoryProductModel.getCountryId());
}
mProgressDialog.dismiss();
}
private boolean getNetworkAvailability() {
return Utils.isNetworkAvailable(getApplicationContext());
}
@Override
public void onClick(int position) {
}
}
E / WindowManager:android.view.WindowLeaked:活动 codes.bala.bmsfinal1.activity.MainActivity已泄漏窗口 最初在此处添加的DecorView @ 52c3fcd [] 在android.view.ViewRootImpl。(ViewRootImpl.java:418) 在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331) 在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) 在android.app.Dialog.show(Dialog.java:322) 在android.app.ProgressDialog.show(ProgressDialog.java:116) 在android.app.ProgressDialog.show(ProgressDialog.java:104) 在codes.bala.bmsfinal1.activity.MainActivity.login(MainActivity.java:61) 在codes.bala.bmsfinal1.activity.MainActivity $ 1.onClick(MainActivity.java:41) 在android.view.View.performClick(View.java:5637) 在android.view.View $ PerformClick.run(View.java:22429) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) I / ViewConfigCompat:在以下位置找不到方法getScaledScrollFactor() ViewConfiguration D / AndroidRuntime:关闭VM
---------崩溃开始E / AndroidRuntime:致命异常:main 进程:codes.bala.bmsfinal1,PID:12077 java.lang.RuntimeException:无法启动活动ComponentInfo {codes.bala.bmsfinal1 / codes.bala.bmsfinal1.activity.InventoryProductActivity}: java.lang.NullPointerException:尝试调用接口方法 'retrofit2.Call codes.bala.bmsfinal1.iinterface.WarehouseService.getAllInventoryProducts()' 在空对象引用上 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
由于:java.lang.NullPointerException:尝试调用接口 方法'retrofit2.Call codes.bala.bmsfinal1.iinterface.WarehouseService.getAllInventoryProducts()' 在空对象引用上 在codes.bala.bmsfinal1.activity.InventoryProductActivity.getFeed(InventoryProductActivity.java:88) 在code.bala.bmsfinal1.activity.InventoryProductActivity.loadInventoryProductFeed(InventoryProductActivity.java:70) 在codes.bala.bmsfinal1.activity.InventoryProductActivity.onCreate(InventoryProductActivity.java:46) 在android.app.Activity.performCreate(Activity.java:6679) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 应用已终止。
答案 0 :(得分:1)
getWarehouseServiceInventoryProduct()
方法返回null
。
您应该在此方法内设置断点并检查发生了什么(或向此方法添加一些日志)。