Problem in the Fragment

Asked

Viewed 35 times

0

I have a class that uses fragment, but I’m having trouble calling one of these activity. When I do it for my own App. I want to click on the fragment MsgFragment.

Below follows the main class and the class I intend to call.

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();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.profile, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {

            // Intent intent = new Intent(this,SairActivity.class);
            //  startActivity(intent);
            AuthUI.getInstance()
                    .signOut(this)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        public void onComplete(@NonNull Task<Void> task) {
                            // user is now signed out
                            finish();
                        }
                    });

            return true;

        }

        return super.onOptionsItemSelected(item);
    }

    private void disableNavigationViewScrollbars(NavigationView navigationView) {
        if (navigationView != null) {
            NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0);
            if (navigationMenuView != null) {
                navigationMenuView.setVerticalScrollBarEnabled(false);
            }
        }
    }
    // implemntação de Firebase cloud messrgr


//-------------------------------------------------------------------


} 

The class I want to appear after clicking on MsgFragment

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();
    }

}
  • To display another activity use the startActivity( new Intent(getContext(), MainActivity.class) );

  • Valdeir Psr did not work the App continues to stop

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.