当WIFI连接中断时,我检测到WIFI连接。
它会在所有小部件的顶部显示framelayout
。
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);
}
xml的代码如下:
<?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>
在致电reconnect_fragment();
时,它会在java.lang.NullPointerException
显示reconnect_layout.setVisibility(View.VISIBLE);
有没有错?
如何解决这个问题?
答案 0 :(得分:2)
正如您在评论中所述,framelayout是片段布局
所以改为
reconnect_layout = (FrameLayout) view.findViewById(R.id.reconnect_layout);
如果它在活动xml中,则使用getActivity().findViewById
答案 1 :(得分:1)
替换
reconnect_layout = (FrameLayout) getActivity().findViewById(R.id.reconnect_layout);
与
reconnect_layout = (FrameLayout) view.findViewById(R.id.reconnect_layout);