Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – my button inside fragment activity doesn’t work

Posted on November 11, 2022 By

Solution 1 :

you can try this it works for me. Override the onActivityCreated() method and do this.

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Button btnHospital = getActivity().findViewById(R.id.BtnInfoHospital);
        /// All buttons

        btnHospital.setOnClickListener(this);
        // All buttons
    }

I hope it helps you regards

Problem :

I have a 3 fragment, and in the last fragment I have some buttons to redirect user to open a link,
enter image description here
but it doesn’t work. here is my fragment activity

package com.vyzyz.covidupdate.fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.vyzyz.covidupdate.R;


/**
* A simple {@link Fragment} subclass.
*/
public class InfoFragment extends Fragment implements View.OnClickListener {

   public InfoFragment() {
       // Required empty public constructor
   }


   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       // Inflate the layout for this fragment
       View v = inflater.inflate(R.layout.fragment_info, container, false);


       Button btnHospital = (Button)v.findViewById(R.id.BtnInfoHospital);
       Button btnSymptom = (Button)v.findViewById(R.id.BtnInfoSymptom);
       Button btnPrevention = (Button)v.findViewById(R.id.BtnInfoPrevention);
       Button btnAdviceWho = (Button)v.findViewById(R.id.BtnInfoAdviceWho);

       btnHospital.setOnClickListener(this);
       btnSymptom.setOnClickListener(this);
       btnPrevention.setOnClickListener(this);
       btnAdviceWho.setOnClickListener(this);
       return v;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       if (item.getItemId() == android.R.id.home) {

       }
       return super.onOptionsItemSelected(item);
   }

   @Override
   public void onClick(View v) {
       switch (v.getId()) {
           case R.id.btnInfoHospital:
               String hospitalUrl = "https://news.detik.com/berita/d-4942353/daftar-rumah-sakit-rujukan-covid-19-seluruh-indonesia?single=1";
               Uri hospitalUri = Uri.parse(hospitalUrl);
               Intent intentInfoHospital = new Intent(Intent.ACTION_VIEW, hospitalUri);
              // Toast.makeText(this, getResources().getString(R.string.redirect), Toast.LENGTH_SHORT).show();
               startActivity(intentInfoHospital);
               break;

           case R.id.btnInfoSymptom:
               String symptomUrl = "https://www.cnnindonesia.com/gaya-hidup/20200128205625-258-469589/infografis-bedanya-demam-selesma-dan-virus-corona-wuhan";
               Uri symptomUri = Uri.parse(symptomUrl);
               Intent intentInfoSymptom = new Intent(Intent.ACTION_VIEW, symptomUri);
               //Toast.makeText(this, getResources().getString(R.string.redirect), Toast.LENGTH_SHORT).show();
               startActivity(intentInfoSymptom);
               break;

           case R.id.btnInfoPrevention:
               String urlPrevention = "https://www.kompas.com/sains/read/2020/03/15/190200123/panduan-mencegah-virus-corona-pesan-who-untuk-kita-semua?page=all#page4";
               Uri preventionUri = Uri.parse(urlPrevention);
               Intent intentPrevention = new Intent(Intent.ACTION_VIEW, preventionUri);
               //Toast.makeText(this, getResources().getString(R.string.redirect), Toast.LENGTH_SHORT).show();
               startActivity(intentPrevention);
               break;

           case R.id.btnInfoAdviceWho:
               String urlWho = "https://www.youtube.com/watch?v=bPITHEiFWLc&feature=emb_title";
               Uri whoUri = Uri.parse(urlWho);
               Intent intentWho = new Intent(Intent.ACTION_VIEW, whoUri);
              // Toast.makeText(this, getResources().getString(R.string.redirect), Toast.LENGTH_SHORT).show();
               startActivity(intentWho);
               break;
       }
   }
}

and here is my main, activity

package com.vyzyz.covidupdate.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import com.vyzyz.covidupdate.R;
import com.vyzyz.covidupdate.fragment.IdnFragment;
import com.vyzyz.covidupdate.fragment.InfoFragment;
import com.vyzyz.covidupdate.fragment.SummaryFragment;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

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

        //Menampilkan Fragment Summary Ketika App Dibuka
        SummaryFragment summaryFragment = new SummaryFragment();
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.main_frame,summaryFragment)
                .commit();

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNav);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);


    }

    //Menu Navigasi Bawah
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            //Ke Fragment Summary
            case R.id.summaryMenu:
                SummaryFragment summaryFragment = new SummaryFragment();
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.main_frame,summaryFragment)
                        .commit();
                return true;

            //Ke Fragment Idn
            case R.id.summaryIdnMenu:
                IdnFragment idnFragment = new IdnFragment();
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.main_frame,idnFragment)
                        .commit();
                return true;

            //ke Fragment News
            case R.id.infoMenu:
              InfoFragment infoFragment = new InfoFragment();
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.main_frame,infoFragment)
                        .commit();
                return true;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //menampilkan menu utama
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Memilih Masing-Masing Menu pada main menu
        switch (item.getItemId()) {
           //Ke activity About
           case R.id.aboutMenu:
                Intent intentAbout = new Intent(this, AboutActivity.class);
                startActivity(intentAbout);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}


i’ve tried to move that fragment to basic activity and the button is work, but, when turn back to fragment, the button doesn’t work anymore. I’ve searching same problem in this site, but it cannot solve my problem.
Anyone can please help me?

READ  [FIXED] java - 'Person()' has private access in 'android.app.Person'
Powered by Inline Related Posts
Android Tags:android, android-button, android-fragments, android-intent, java

Post navigation

Previous Post: [FIXED] android – How to achieve ionic permissions on first load
Next Post: [FIXED] android – How do i put online database into local database

Related Posts

[FIXED] Android emulator is launched but not attached to the development environment? Android
[FIXED] Can Admob be integrated in Android TV Android
[FIXED] java – Swig Error when using C functions in Kotlin – two different errors Android
[FIXED] Why can’t I see the learn option on Android Studio? Android
[FIXED] android – Exoplayer seekbar preview Android
[FIXED] android – Flutter_map widget returns a black screen when called? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme