我正在片段中实现两个按钮,点击后会转移到两个不同的活动。但是在点击时,没有任何反应。该应用程序不会强制关闭,也不会调试器显示任何异常。使用Log.d我发现onClick没有被调用。 我已经将这篇文章推荐用于实施,但它不起作用Multiple Buttons In Fragment Class Issue?
这是我的代码
TheOtherFragment.java
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;
}
}
}
答案 0 :(得分:1)
您必须返回v
而不是inflater.inflate(R.layout.fragment_the_other, container, false);
,inflater.inflate
会返回一个新对象,并且您还没有注册onClickListener