我的片段 Activity 中的按钮不起作用

2020年12月2日 57点热度 0条评论

我有一个3片段,在最后一个片段中,我有一些按钮可以重定向用户以打开链接,
enter image description here
但这不起作用。这是我的片段活动

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

这是我的主要活动

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


我曾尝试将该片段移至基本活动,并且该按钮正常工作,但是当回到片段时,该按钮不再起作用。我在此站点中搜索了相同的问题,但无法解决我的问题。

有人可以帮我吗?

解决方案如下:

您可以尝试一下,它对我有用。重写onActivityCreated()方法并执行此操作。

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

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

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

希望对您有帮助