我试图在Android中实现片段通信,如android指南中的那个http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
但我的应用程序崩溃,因为getSupportFragmentManager().findFragmentById()返回null.我的实施有什么问题.
代码如下:
该程序只是通过第一个fragmnet上的按钮单击将输入文本从一个片段发送到另一个片段textView区域.我有一个activity_main.xml和两个片段布局(两个单独的xml文件而不是activity_main.xml中的一部分)
Frag1.java
public class Frag1 extends Fragment {
public Frag1(){
}
buttonClickListener buttonListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
buttonListener = (buttonClickListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnButtonPressListener");
}
}
View myFragmentView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.frag1, container, false);
//SetValue Button
Button setValueButton = (Button) myFragmentView.findViewById(R.id.setValueButton);
setValueButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
buttonListener.onButtonPressed("Message received"); …Run Code Online (Sandbox Code Playgroud) 在考虑具有enum和int作为case参数的switch案例的性能时,最好使用哪个代码片段:
一个.
switch ((ToolbarButton)BtnId)
{
case ToolbarButton.SHOWPROPERTYDIALOG:
OnShowProperties();
break;
case ToolbarButton.MOVETOFIRST:
OnFirstMessage();
break;
case ToolbarButton.MOVETOLAST:
OnLastMessage();
break;
}
Run Code Online (Sandbox Code Playgroud)
B.
switch (BtnId)
{
case (int)ToolbarButton.SHOWPROPERTYDIALOG:
OnShowProperties();
break;
case (int)ToolbarButton.MOVETOFIRST:
OnFirstMessage();
break;
case (int)ToolbarButton.MOVETOLAST:
OnLastMessage();
break;
}
Run Code Online (Sandbox Code Playgroud) 我需要像计时器一样更新KineticJS文本的text属性.但我的下面的代码正如预期的那样工作,我做错了什么:
var dateTimeText = new Kinetic.Text({
x: 40,
y: 400,
text: "Sample",
fontSize: 18,
width: 700,
fontFamily: 'Calibri',
fill: 'black',
width: 700,
padding: 10,
align: 'right',
draggable: true
});
setInterval(function () { onUpdateTime() }, 1000);
function onUpdateTime() {
var date = new Date();
dateTimeText.setText(date.toLocaleTimeString());
}
Run Code Online (Sandbox Code Playgroud)
在onUpdateTime()中,我应该对dateTimeText进行操作吗?在dateTimeText()上添加draggable属性并单击它时,我至少可以看到更新的值:)