我在片段活动中的按钮不起作用
Posted
技术标签:
【中文标题】我在片段活动中的按钮不起作用【英文标题】:my button inside fragment activity doesn't work 【发布时间】:2020-09-27 03:48:50 【问题描述】:我有一个 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);
我已尝试将该片段移至基本活动,并且按钮可以使用,但是当返回片段时,该按钮不再起作用。我在这个网站上搜索过同样的问题,但它不能解决我的问题。 谁能帮帮我?
【问题讨论】:
【参考方案1】:你可以试试这个对我有用。覆盖 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
希望对你有帮助
【讨论】:
以上是关于我在片段活动中的按钮不起作用的主要内容,如果未能解决你的问题,请参考以下文章