无法解析法'findViewById(int)“在片段 [英] Cannot resolve method 'findViewById(int)' in Fragment
问题描述
我想实现以使用的Soundpool以播放声音与一个按钮一个按钮变成碎片。此刻的playSound1快到了为从未使用过,我已经试图执行onClick的方法,但它现在已经来了说,它不能解析方法。我怎么了的Soundpool链接到一个按钮,一个片段?这是java文件
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
Clubb1 =新的Soundpool(10,AudioManager.STREAM_MUSIC,1);
clubb1Id = Clubb1.load(getActivity(),R.raw.clubb1,1); // TODO自动生成方法存根
返回inflater.inflate(R.layout.fragment_one_layout,集装箱,FALSE); 按钮按钮a =(按钮)findViewById(R.id.buttonA); buttonA.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(视图v){ } 公共无效playSound1()
{Clubb1.play(clubb1Id,1,1,1,0,1);}
});
更改方法:
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
Clubb1 =新的Soundpool(10,AudioManager.STREAM_MUSIC,1);
clubb1Id = Clubb1.load(getActivity(),R.raw.clubb1,1); 查看rootView = inflater.inflate(R.layout.fragment_one_layout,集装箱,FALSE); 按钮按钮a =(按钮)rootView.findViewById(R.id.buttonA); buttonA.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
Clubb1.play(clubb1Id,1,1,1,0,1);
} }); 返回教职员;
}
您做了很多错误的位置。因此,林不知道我的方法帮你就够了)写你的程序导致成注释,we`ll尝试更多。
I am trying to implement a button into a fragment in order to use the soundPool in order to play a sound with a button. At the moment the playSound1 is coming up as never used and I have tried to implement the onClick Method but it is now coming up saying it cannot resolve the method. How do I link the soundPool to a button in a fragment? this is the .java file
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_one_layout, container, false);
Button buttonA = (Button) findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
public void playSound1()
{Clubb1.play(clubb1Id,1,1,1,0,1);}
});
Change your method to :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
View rootView = inflater.inflate(R.layout.fragment_one_layout, container, false);
Button buttonA = (Button) rootView.findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Clubb1.play(clubb1Id, 1, 1, 1, 0, 1);
}
});
return veiw;
}
You did a lot of mistakes here. So im not sure that my approach help you enough ) Write your program result into comment and we`ll try more.
这篇关于无法解析法'findViewById(int)“在片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!