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?