我想在按钮点击时将数据从Activity传递到当前加载的片段。 为此,我创建了Interface并定义为活动并在接口中实现,但无法在片段中获得响应。
每当按钮在Activity中点击时,我都需要输出。
这是我的代码。请帮我解决这个问题。
public interface Click {
void onClick(String msg);
}
public class MainActivity extends FragmentActivity {
DrawerLayout dlMain;
Button btnClick;
Click click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentOne fragmentOne = new FragmentOne();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.flContainer, fragmentOne,"FragmentOne");
ft.commit();
click = (Click) getSupportFragmentManager().findFragmentByTag("FragmentOne").;
dlMain = (DrawerLayout) findViewById(R.id.dlMain);
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Btn Click", "click");
click.onClick("Button Clicked!");
}
});
}
}
public class FragmentOne extends Fragment implements Click {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
@Override
public void onClick(String msg) {
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
}
}
但是我在MainActivity中遇到错误 这是我的错误日志。
05-09 10:30:49.092 19101-19101 / com.psk.interfacetest E / Btn点击:点击 05-09 10:30:49.095 19101-19101 / com.psk.interfacetest E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.psk.interfacetest,PID:19101 显示java.lang.NullPointerException 在com.psk.interfacetest.MainActivity $ 1.onClick(MainActivity.java:37) 在android.view.View.performClick(View.java:4471) 在android.view.View $ PerformClick.run(View.java:18778) 在android.os.Handler.handleCallback(Handler.java:808) 在android.os.Handler.dispatchMessage(Handler.java:103) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:5345) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:824) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:1)
你的情况下,片段替换无法完成。所以你应该从fragment注册接口。你可以检查这段代码。
public class AActivity extends FragmentActivity {
Click click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentOne fragmentOne = new FragmentOne();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.flContainer, fragmentOne,"FragmentOne");
ft.commit();
DrawerLayout dlMain = (DrawerLayout)findViewById(R.id.dlMain);
Button btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Btn Click", "click");
click.onClick("Button Clicked!");
}
});
}
public void setCallback(Click callback) {
this.click=callback;
}
public interface Click {
void onClick(String msg);
}
}
public class AFragment extends Fragment implements AActivity.Click {
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (getActivity() != null) {
((AActivity)getActivity()).setCallback(this);
}
}
@Override
public void onClick(String msg) {
}
}
答案 1 :(得分:1)
try this
public class MainActivity extends FragmentActivity {
DrawerLayout dlMain;
Button btnClick;
Click click;
private FragmentOne fragmentOne;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentOne = new FragmentOne();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.imageTitleTextView, fragmentOne, "FragmentOne");
ft.commit();
//click = (Click) getSupportFragmentManager().findFragmentByTag("FragmentOne");
dlMain = (DrawerLayout) findViewById(R.id.dlMain);
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Btn Click", "click");
fragmentOne.listener.onClick("Button Clicked!");
}
});
}
}
public class FragmentOne extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
Click listener = new Click() {
@Override
public void onClick(String msg) {
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
}
};
}