0

I detect the WIFI connect , when WIFI connect is break.

It will show the framelayout on the top of all widgets.

    public class MjpegPlayerFragment extends Fragment {

    public static FrameLayout reconnect_layout;


private class GetTimeStamp extends AsyncTask<URL, Integer, String> {

        protected void onPostExecute(String result) {
            Activity activity = getActivity() ;
            if (activity != null) {
                reconnect_fragment();

            }
            super.onPostExecute(result) ;
    }
}

         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View view = inflater.inflate(R.layout.preview_player, container, false) ;
                    reconnect_layout = (FrameLayout) getActivity().findViewById(R.id.reconnect_layout);
                    return view ;
    }


    public static void reconnect_fragment() {
        reconnect_layout.setVisibility(View.VISIBLE);
    }

And the code of xml is like the following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/reconnect_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:background="#454749">

       <RelativeLayout
        android:id="@+id/layout_imgbtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:layout_above="@+id/bottom_buttonlayout"
        android:layout_below="@+id/top_buttonlayout"
        android:orientation="horizontal" >

        <ImageView 
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/connect"
            android:layout_centerVertical="true"
            android:background="@drawable/phone"/>

        <ImageView 
            android:id="@+id/connect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/break1"/>

         <ImageView 
            android:id="@+id/break_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/connect"
            android:layout_centerVertical="true"
            android:background="@drawable/camera_break"
            />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/Reconnect_button_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_below="@+id/layout_break_text"
        android:layout_centerInParent="true"
        android:orientation="horizontal" >


        <ImageButton
            android:id="@+id/Imgbtn_reconnect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_weight="1"
            android:background="#00000000"
            android:src="@drawable/reconnect" />


    </LinearLayout>

    <FrameLayout
        android:id="@+id/player_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

       <FrameLayout
            android:id="@+id/player_surface_frame"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center" >

            <SurfaceView
                android:id="@+id/player_surface"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center" />

        </FrameLayout>
    </FrameLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/top_buttonlayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#444444" >

            <ImageView 
                android:id="@+id/imageBackground"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="@drawable/logo"
                />


            <ImageButton
                android:id="@+id/SettingButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:scaleType="fitXY"
                android:background="@drawable/parmeter_setting" />



            <ImageButton
                android:id="@+id/FileSavebutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:scaleType="fitXY"
                android:background="@drawable/save_in_camera" />
    </LinearLayout>

</RelativeLayout>

When it call the reconnect_fragment(); it show the java.lang.NullPointerException at reconnect_layout.setVisibility(View.VISIBLE);

Does there has any wrong ?

How to solve this problem ?

Wun
  • 6,211
  • 11
  • 56
  • 101

2 Answers2

2

As you said in your comment framelayout is in fragment layout

So Change to

 reconnect_layout = (FrameLayout) view.findViewById(R.id.reconnect_layout);

If it is in activity xml you use getActivity().findViewById

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @MartinWun why do you require a fragmelayout in the first place?. I would use it as a container to add the fragments. – Raghunandan Jan 10 '14 at 08:04
  • You mean add a new fragment for this fragment ? – Wun Jan 10 '14 at 09:24
  • @MartinWun why do you need framelayout in your fragment xml. It is basicallyd used as container. or used to place one view above the other. – Raghunandan Jan 10 '14 at 09:35
  • If I don't use framelaout , can I show the button on the top of other layout ? – Wun Jan 11 '14 at 15:59
  • @MartinWun if you want to overlay a view on top of another use RelativeLayout or FrameLayout – Raghunandan Jan 11 '14 at 16:01
  • I use the FrameLayout like the above xml file , but it doesn't overlay a view on top of another. Do you know why ? – Wun Jan 11 '14 at 16:10
  • http://stackoverflow.com/questions/16793660/overlay-image-over-another-android. refer this. and using framelayout there is 1link whichi will try to find for you – Raghunandan Jan 11 '14 at 16:13
  • @MartinWun http://stackoverflow.com/questions/4182486/placing-overlappingz-index-a-view-above-another-view-in-android – Raghunandan Jan 11 '14 at 16:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45013/discussion-between-raghunandan-and-martin-wun) – Raghunandan Jan 11 '14 at 16:14
1

replace

reconnect_layout = (FrameLayout) getActivity().findViewById(R.id.reconnect_layout);

with

reconnect_layout = (FrameLayout) view.findViewById(R.id.reconnect_layout);
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43