N
N
natan_892018-08-24 02:22:31
Java
natan_89, 2018-08-24 02:22:31

Error in the loadData method, how to fix it?

Hello!
To be honest, I ran into an error, and I can not understand what's wrong.
I'm making an application, a quiz.
When I try to load the menu from MainActivity and call the loadData method, the application only loads the splash screen and that's it, it immediately crashes.
When I commented out the line with the call method, the application is loaded, but naturally the menu that I am trying to load with this method does not work.
Here is the actual code, with loadData commented out

public class MainActivity extends BaseActivity {

    private Activity activity;
    private Context context;

    private Toolbar toolbar;

    private AccountHeader accountheader=null;
    private Drawer drawer=null;

    private ArrayList<CategoryModel> categoryList;
    private CategoryAdapter adapter=null;
    private RecyclerView recyclerView;

    private AccountHeader header;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=(Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        recyclerView=(RecyclerView) findViewById(R.id.rvContent);
        recyclerView.setLayoutManager(new GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false));


        categoryList = new ArrayList<>();
        adapter=new CategoryAdapter(context, activity, categoryList);
        recyclerView.setAdapter(adapter);

        activity=MainActivity.this;
        context=getApplicationContext();

        initLoader();
        //loadData();

        final IProfile profile=new ProfileDrawerItem().withIcon(R.drawable.ic_dev);

        header=new AccountHeaderBuilder().withActivity(this).withTranslucentStatusBar(true).withHeaderBackground(R.drawable.header).withOnAccountHeaderProfileImageListener(new AccountHeader.OnAccountHeaderProfileImageListener() {
            @Override
            public boolean onProfileImageClick(View view, IProfile profile, boolean current) {
                ActivityUtilities.getInstance().invokeCustomUrlActivity(activity, CustomUrlActivity.class, getResources().getString(R.string.site), getResources().getString(R.string.site_url), false);
                return false;
            }

            @Override
            public boolean onProfileImageLongClick(View view, IProfile profile, boolean current) {
                return false;
            }
        }).addProfiles(profile).build();

        drawer=new DrawerBuilder().withActivity(this).withToolbar(toolbar).withHasStableIds(true).withAccountHeader(header).addDrawerItems(new PrimaryDrawerItem().withName("О приложении").withIcon(R.drawable.ic_dev).withIdentifier(10).withSelectable(false),

                new SecondaryDrawerItem().withName("Instagram").withIcon(R.drawable.ic_instagram).withIdentifier(20).withSelectable(false), new SecondaryDrawerItem().withName("Google+").withIcon(R.drawable.ic_google_plus).withIdentifier(23).withSelectable(false),

                new DividerDrawerItem(), new SecondaryDrawerItem().withName("Настройки").withIcon(R.drawable.ic_settings).withIdentifier(30).withSelectable(false), new SecondaryDrawerItem().withName("Оцените приложение").withIcon(R.drawable.ic_rating).withIdentifier(31).withSelectable(false), new SecondaryDrawerItem().withName("Поделитесь").withIcon(R.drawable.ic_share).withIdentifier(32).withSelectable(false), new SecondaryDrawerItem().withName("Соглашения").withIcon(R.drawable.ic_privacy_policy).withIdentifier(33).withSelectable(false),

                new DividerDrawerItem(), new SecondaryDrawerItem().withName("Выход").withIcon(R.drawable.ic_exit).withIdentifier(40).withSelectable(false))

                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {

                        if (drawerItem != null) {
                            Intent intent=null;
                            if (drawerItem.getIdentifier() == 10) {
                                ActivityUtilities.getInstance().invokeNewActivity(activity, AboutDevActivity.class, false);

                            } else if (drawerItem.getIdentifier() == 20) {
                                AppUtilities.instagramLink(activity);

                            } else if (drawerItem.getIdentifier() == 23) {
                                AppUtilities.googlePlusLink(activity);

                            } else if (drawerItem.getIdentifier() == 30) {
                                //TODO: Invoke SettingsActivity

                            } else if (drawerItem.getIdentifier() == 31) {
                                AppUtilities.rateThisApp(activity);

                            } else if (drawerItem.getIdentifier() == 32) {
                                AppUtilities.shareApp(activity);

                            } else if (drawerItem.getIdentifier() == 33) {
                                ActivityUtilities.getInstance().invokeCustomUrlActivity(activity, CustomUrlActivity.class, getResources().getString(R.string.privacy), getResources().getString(R.string.privacy_url), false);

                            } else if (drawerItem.getIdentifier() == 40) {

                            }
                        }

                        return false;
                    }
                })

                .withSavedInstance(savedInstanceState).withShowDrawerOnFirstLaunch(true).withShowDrawerUntilDraggedOpened(true).build();

    }

    @Override
    public void onBackPressed() {
        if (drawer != null && drawer.isDrawerOpen()) {
            drawer.closeDrawer();
        } else {
            AppUtilities.tapPromToExit(this);
        }
    }

    private void loadData(){
        showLoader();
        loadJson();
    }

    private void loadJson() {
        StringBuffer sb=new StringBuffer();
        BufferedReader br=null;
        try {
            br=new BufferedReader(new InputStreamReader(getAssets().open(AppConstants.CONTENT_FILE)));
            String temp;
            while ((temp=br.readLine()) != null)
                sb.append(temp);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        parseJson(sb.toString());
    }

    private void parseJson(String jsonData) {
        try {
            JSONObject jsonObject=new JSONObject(jsonData);
            JSONArray jsonArray=jsonObject.getJSONArray(AppConstants.JSON_KEY_ITEMS);

            for (int i=0; i < jsonArray.length(); i++) {
                JSONObject object=jsonArray.getJSONObject(i);

                String categoryId=object.getString(AppConstants.JSON_KEY_CATEGORY_ID);
                String categoryName=object.getString(AppConstants.JSON_KEY_CATEGORY_NAME);

                categoryList.add(new CategoryModel(categoryId, categoryName));

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        hideLoader();
        adapter.notifyDataSetChanged();
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question