Problem calling Class Activitity

1

I have a class I want to call another class by clicking on one of the snippets, but it is not responding when calling the other activity gives an error in the app and application stops working. I have the following classes the first where is the fragment that I want to click and the other the MainActivity that I want to access.

public class ProfileActivity extends AppCompatActivity {

    private static final String TAG = "ProfileActivity";
    private FragmentManager fragmentManager;

    private Fragment fragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        fragmentManager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragment = new MenuFragment();
        fragmentTransaction.replace(R.id.main_container_wrapper, fragment);
        fragmentTransaction.commit();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        disableNavigationViewScrollbars(navigationView);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int id = item.getItemId();

                if (id == R.id.nav_perfil) {
                    fragment = new ProfileFragment();

                } else if (id == R.id.nav_menu) {
                    fragment = new MenuFragment();

                } else if (id == R.id.nav_notificacao) {
                    fragment = new MsgFragment();

                } else if (id == R.id.nav_sobre) {
                    fragment = new SobreFragment();

                } else if (id == R.id.nav_sair) {

                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);

                    startActivity(intent);
                }

                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.main_container_wrapper, fragment);
                transaction.commit();

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                assert drawer != null;
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

Following the MainActivity class I want to access.

public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    private PayloadAdapter adapter;

    private PresenceEventListener presenceEventListener;

    private BroadcastReceiver tokenReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateSubtitle();
        }
    };

    private BroadcastReceiver presenceReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateSubtitle();
            supportInvalidateOptionsMenu();
        }
    };
    private View emptyView;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();
        registerReceivers();
        updateSubtitle();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateSubtitle();
    }

    private void registerReceivers() {
        Payload.registerOnSharedPreferenceChanges(this, this);
        LocalBroadcastManager instance = LocalBroadcastManager.getInstance(this);
        instance.registerReceiver(tokenReceiver, TokenFetcher.INTENT_FILTER);
        instance.registerReceiver(presenceReceiver, PresenceEventListener.INTENT_FILTER);
        presenceEventListener = new PresenceEventListener(this);
        presenceEventListener.register();
    }

    private void unregisterReceivers() {
        Payload.unregisterOnSharedPreferenceChanges(this, this);
        LocalBroadcastManager instance = LocalBroadcastManager.getInstance(this);
        instance.unregisterReceiver(tokenReceiver);
        instance.unregisterReceiver(presenceReceiver);
        presenceEventListener.unregister();
    }

    private void initRecyclerView() {
        emptyView = findViewById(R.id.empty_view);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(false);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new PayloadAdapter(Payload.fetchPayloads(this));
        recyclerView.setAdapter(adapter);
        ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
                final int position = viewHolder.getAdapterPosition();
                Payload.remove(MainActivity.this, adapter.getItem(position));
                adapter.removeItemAtPosition(position);
                onAdapterCountMightHaveChanged();
            }
        };
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);
        onAdapterCountMightHaveChanged();
    }

    private void updateSubtitle() {
        //noinspection ConstantConditions
        getSupportActionBar().setSubtitle(FirebaseInstanceId.getInstance().getToken());
    }

    @Override
    protected void onDestroy() {
        unregisterReceivers();
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.findItem(R.id.action_presence).setIcon(presenceEventListener.isConnected() ? android.R.drawable.presence_online : android.R.drawable.presence_invisible);
        menu.findItem(R.id.action_share_token).setVisible(!TextUtils.isEmpty(FirebaseInstanceId.getInstance().getToken()));
        menu.findItem(R.id.action_delete_all).setVisible(adapter != null && adapter.getItemCount() > 0);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_share_token:
                final String token = FirebaseInstanceId.getInstance().getToken();
                if (token != null) {
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    intent.putExtra(Intent.EXTRA_TEXT, token);
                    startActivity(Intent.createChooser(intent, getString(R.string.menu_share_token)));
                }
                return true;
            case R.id.action_delete_all:
                Payload.removeAll(this);
                adapter.removeAll();
                onAdapterCountMightHaveChanged();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (sharedPreferences.contains(key)) {
            final Payload payload = Payload.with(key, sharedPreferences.getString(key, ""));
            adapter.addPayload(payload);
            recyclerView.smoothScrollToPosition(0);
            onAdapterCountMightHaveChanged();
        }
    }

    void onAdapterCountMightHaveChanged() {
        int count = adapter != null ? adapter.getItemCount() : 0;
        emptyView.setVisibility(count > 0 ? View.INVISIBLE : View.VISIBLE);
        supportInvalidateOptionsMenu();
    }
}
    
asked by anonymous 15.02.2018 / 13:06

0 answers