• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Interfragment communication null pointer on view loading

SergF

Lurker
Nov 9, 2015
4
0
Hi, guys,
as you could guess another one newbee question here :)
It should be quite routine thing for seasoned developer, but I'm not that guy.
just trying to grasp the recommended/usual way to do this thing.
So the question is next:
Let's say we have one activity and two fragments, looking like this:

fragment 1:
Code:
/**
* Created by ... on 11/9/2015.
*/
public class MyDetailFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.details_layout, container, false);
return view;
}
public void show(){
TextView view=(TextView)getActivity().findViewById(R.id.detail_text);
view.setText(Long.toString(System.currentTimeMillis()));
}

}
fragment 2:
Code:
/**
* Created by ... on 11/9/2015.
*/
public class MyListFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.list_layout, container, false);
Button button=(Button)view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyListFragmentListener fragmentListener=(MyListFragmentListener)getActivity();
fragmentListener.updateDetails();
}
});
return view;
}
interface MyListFragmentListener{
void updateDetails();
}

}
activity:
Code:
public class Main extends Activity implements MyListFragment.MyListFragmentListener{
    MyDetailFragment details;
    MyListFragment list;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        list=new MyListFragment();
        details=new MyDetailFragment();
        FragmentTransaction ft=getFragmentManager().beginTransaction();
        ft.add(R.id.list_container, list, "LIST");
        if(getResources().getBoolean(R.bool.dual_pane)){

            Log.d("test", "dual_pane is true");
            FrameLayout listFrame=(FrameLayout)findViewById(R.id.list_container);
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            listFrame.setLayoutParams(params);
            ft.add(R.id.detail_container, details, "DETAILS").commit();

        }else{
            Log.d("test","dual_pane is false");
            ft.commit();
        }
    }

    @Override
    public void updateDetails(){

        if(getResources().getBoolean(R.bool.dual_pane)) {

            details.show();
        }else{
            FragmentTransaction ftr=getFragmentManager().beginTransaction();
            ftr.replace(R.id.list_container, details).addToBackStack(null).commit();
           //if uncomment section below, it works, but looks wrong...
           // Runnable r = new Runnable() {
           //     @Override
           //     public void run(){
                    details.show(); //<-- put your code in here.
           //     }
           // };
           // android.os.Handler handler=new android.os.Handler();
           // handler.postDelayed(r, 3000);

        }
    }
}
It should show two fragments and display currentTimeMillis() in fragment 1, after click on button in fragment 2 in landscape mode, and replace fragment 2 with fragment 1, displaying the same currentTimeMillis() in fragment 1 in portrait mode.

It works well in landscape, and kind of work in portrait, but
take a look at this mess in the end of activity code, if uncomment this thing with delaying to run details.show() method - it works, but such solution looks wrong...

If thing commented (like in current listing) it crushes in portrait because of NullPointerException in this string of fragment 1:
Code:
TextView view=(TextView)getActivity().findViewById(R.id.detail_text);
So the question: Is there recommended/common solution for such task, that looks less terrible?
Sorry, for newbee question...
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones