实际上,在我找到它之前,从未在这里寻求帮助,但这是例外情况。 我的应用程序应获取json文件(单个文件,基本上是静态信息),将其解析并将其放入不同片段的navigationDrawer中。 NavDrawer应该动态填充(json具有三个参数,其中之一是单个项的名称),内容应以不同的片段显示,并且所有这些都应同时适用于纵向和横向(应保存状态) 。
所以我得到了NetworkOperations.java类,在其中我尝试使用enqueue()类型的请求从放置json文件的url获得响应。由于这种类型,基本上,据我了解,我的所有逻辑都应使用onResponse方法,因为在其他情况下,由于此请求工作的异步类型,在大多数情况下,我将获得NPE。
要保存我的响应列表类型为List的数据,我试图通过意图进行传输。我的问题是,在getAplicationContext()(下面的代码)期间出现了NullPointerException。有人可以提供帮助吗? onCreate方法正在执行,就像声明了retrofitBuilder的App util方法一样。从理论上讲,这两种情况都应该出现,为什么我不能拥有它?
先谢谢您,抱歉我的英语不好。
编辑:这是提供NPE的代码
Intent intent = new Intent(mContext.getApplicationContext() ,
MainActivity.class);
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//Tags
private static final String TAG = "MainActivity";
//vars
private NetworkOperations operations = new NetworkOperations();
private List<com.example.coder.testapp.model.Menu> menuItemList = new ArrayList<>();
private ArrayList<com.example.coder.testapp.model.Menu> listForDrawer = new ArrayList<>();
//widgets
private NavigationView navigationView;
private ProgressBar mProgressBar;
private Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mProgressBar = findViewById(R.id.progress_bar);
new DownloadTask().execute();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == FragmentKeys.FRAGMENT_ONE) {
} else if (id == FragmentKeys.FRAGMENT_TWO) {
} else if (id == FragmentKeys.FRAGMENT_THREE) {
} else if (id == FragmentKeys.FRAGMENT_FOUR) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void addNewItem (String itemName){
menu = navigationView.getMenu();
menu.add(Menu.NONE,Menu.NONE,Menu.NONE,itemName);
System.out.println("Item " + itemName + " added" );
}
public void initDrawer(List<com.example.coder.testapp.model.Menu> list){
System.out.println(list);
if (list != null) {
System.out.println(list.size());
addNewItem(list.get(FragmentKeys.FRAGMENT_ONE).getName());
addNewItem(list.get(FragmentKeys.FRAGMENT_TWO).getName());
addNewItem(list.get(FragmentKeys.FRAGMENT_THREE).getName());
addNewItem(list.get(FragmentKeys.FRAGMENT_FOUR).getName());
}
}
private void getMenuList (){
menuItemList = new ArrayList<>();
operations.getResponses(menuItemList);
Intent intent = new Intent(this, MainActivity.class);
listForDrawer = intent.getParcelableArrayListExtra("menus");
System.out.println("get menu list response " + menuItemList);
}
class DownloadTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
Log.d(TAG, "onPreExecute: ");
publishProgress(true);
}
@Override
protected Void doInBackground(Void... voids) {
Log.d(TAG, "doInBackground: ...");
getMenuList();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Log.d(TAG, "onPostExecute: completed...");
initDrawer(listForDrawer);
publishProgress(false);
}
public void publishProgress(boolean change){
if (change) {
mProgressBar.bringToFront();
mProgressBar.setBackgroundColor(getResources().getColor(R.color.mainBackgroundblur));
mProgressBar.setVisibility(View.VISIBLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
} else {
mProgressBar.setVisibility(View.GONE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
}
}
NetworkOperations.java
public class NetworkOperations {
private static final String TAG = "Network operations";
private ArrayList<Menu> menuList;
private Example mRespons;
private Context mContext;
public void getResponses(final List<Menu> list) {
App.getServerAPI().getExample().enqueue(new Callback<Example>() {
@Override
public void onResponse(@NonNull Call<Example> call,
@NonNull Response<Example> response) {
if (response.body()!= null) {
mRespons = response.body();
System.out.println("Response is not empty " + mRespons);
if (mRespons != null) {
System.out.println(mRespons.getMenu() + " shit");
list.addAll(mRespons.getMenu());
menuList= new ArrayList<>();
menuList.addAll(list);
System.out.println("This is global " + menuList);
System.out.println("This is inner " + list);
if (menuList.size()>0) {
System.out.println(mContext.getApplicationContext().toString());
Intent intent = new Intent(mContext.getApplicationContext() , MainActivity.class);
intent.putParcelableArrayListExtra("menus", menuList);
System.out.println("OLA " + menuList);
}
}
}
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
Toast.makeText(mContext, "An error occurred during networking", Toast.LENGTH_SHORT).show();
}
});
}
}
编辑: 错误代码
07-30 20:50:38.715 4751-4751/com.example.coder.testapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.coder.testapp, PID: 4751
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at com.example.coder.testapp.operations.NetworkOperations$1.onResponse(NetworkOperations.java:51)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)