Android 按钮在片段中不工作
我在一个片段中实现了两个按钮,点击后将转移到两个不同的活动。但是,单击时,什么也没有发生。应用程序不会强制关闭,调试器也不会显示任何异常。使用Log.d,我发现onClick没有被调用。 我已经推荐了这篇文章来实现它,但是它不起作用 这是我的密码 TheOtherFragment.javaAndroid 按钮在片段中不工作,android,button,android-activity,onclick,fragment,Android,Button,Android Activity,Onclick,Fragment,我在一个片段中实现了两个按钮,点击后将转移到两个不同的活动。但是,单击时,什么也没有发生。应用程序不会强制关闭,调试器也不会显示任何异常。使用Log.d,我发现onClick没有被调用。 我已经推荐了这篇文章来实现它,但是它不起作用 这是我的密码 TheOtherFragment.java package com.tct.level4; import android.content.Context; import android.content.Intent; import android.os
package com.tct.level4;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class TheOtherFragment extends Fragment implements View.OnClickListener {
Context context;
private Button sound;
private Button vib;
View v;
public TheOtherFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_the_other, container, false);
context = container.getContext();
sound = (Button) v.findViewById(R.id.sound);
vib = (Button) v.findViewById(R.id.vibration);
Log.d("onactivity", "called");
sound.setOnClickListener(this);
vib.setOnClickListener(this);
return inflater.inflate(R.layout.fragment_the_other, container, false);
}
@Override
public void onClick(View va) {
switch (va.getId()) {
case R.id.vibration:
Log.d("vib","1");
Intent intent = new Intent(context, Vib.class);
startActivity(intent);
Log.d("vib","2");
break;
case R.id.sound:
Log.d("sound","1");
Intent inten = new Intent(context, Ring.class);
startActivity(inten);
Log.d("sound","2");
break;
}
}
}
您必须返回
v
而不是充气机。充气(R.layout.fragment\u the_the_other,container,false)
,inflater.inflate
返回一个新对象,并且您尚未在该对象上注册onClickListener