我有一个包含两个片段的活动。其中一个片段mainFragment是整个屏幕的大小。还有另一个片段summaryFrag,它包含在mainFragment的视图中。我正在尝试根据mainFragment中发生的操作更新summaryFrag上的文本。
目标:在mainFragment中按下按钮时,将summaryFrag中TextView的文本设置为某个内容。
我尝试通过使用
从mainFragment内部获取TextView来实现此目的 TextView textView = (TextView) summary.getView().findViewById(R.id.prev_guess_txt);
但是我得到一个空指针异常错误
FATAL EXCEPTION: main
Process: com.example.damia.bullseyev3, PID: 6049
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.damia.bullseyev3.fragments.mainFragment.updatePreviousGuesses(mainFragment.java:83)
at com.example.damia.bullseyev3.fragments.mainFragment$1.onClick(mainFragment.java:68)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
mainFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
scrollView = (ScrollView) v.findViewById(R.id.scroll);
ll = (LinearLayout)v.findViewById(R.id.linearlayout);
submitBtn = (Button)v.findViewById(R.id.submit_guess_btn);
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updatePreviousGuesses();
}
});
return v;
}
public void updatePreviousGuesses(){
//TODO add method to check if guess is valid first before updating the scrollview
FrameLayout holder = new FrameLayout(getActivity());
holder.setId(R.id.holder);
summaryFrag summary = summaryFrag.newInstance("","");
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.holder, summary).commit();
TextView textView = (TextView) summary.getView().findViewById(R.id.prev_guess_txt);
textView.setText("test");
ll.addView(holder, 0);
}
summaryFrag.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.summary, container, false);
return v;
}
}
(summaryFrag目前没有改变默认生成的片段代码)
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
mainFragment mainfrag = (mainFragment)fm.findFragmentById(R.id.fragment_container);
if(mainfrag == null){
mainfrag = mainFragment.newInstance("","");
fm.beginTransaction().add(R.id.fragment_container, mainfrag).commit();
}
}
}
我已经阅读了一些有关此问题的解决方案,我知道这是由于片段生活方式和一切,但我仍然试图自己解决它并且没有成功。我已经尝试过将一些东西移到onViewCreated上,但我不知道它是如何工作得足以知道应该在那里移动什么,不应该移动什么。
我是新手回调和所有内容,所以如果您的解决方案包含它们,请稍后向初学者解释一下:)
由于
答案 0 :(得分:0)
更好地使用setarguments&当你试图在片段之间进行交互时,会得到getarguments