我在加载片段活动时意外收到空指针异常错误。我指的是FrameLayout将用户引导到另一个活动(点击按钮时)。
11-10 07:31:58.773: E/AndroidRuntime(10340): FATAL EXCEPTION: main
11-10 07:31:58.773: E/AndroidRuntime(10340): java.lang.NullPointerException
11-10 07:31:58.773: E/AndroidRuntime(10340): at com.dooba.beta.ThirdFragment.onCreateView(ThirdFragment.java:22)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.os.Handler.handleCallback(Handler.java:615)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.os.Handler.dispatchMessage(Handler.java:92)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.os.Looper.loop(Looper.java:137)
11-10 07:31:58.773: E/AndroidRuntime(10340): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-10 07:31:58.773: E/AndroidRuntime(10340): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 07:31:58.773: E/AndroidRuntime(10340): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 07:31:58.773: E/AndroidRuntime(10340): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-10 07:31:58.773: E/AndroidRuntime(10340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-10 07:31:58.773: E/AndroidRuntime(10340): at dalvik.system.NativeStart.main(Native Method)
以下是活动代码:
public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_city, container, false);
FrameLayout icity = (FrameLayout)getView().findViewById(R.id.flCityBac);
icity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
}
});
return rootView;
}
}
下面的是布局代码:
<RelativeLayout
android:id="@+id/layout_gridview_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:alpha="1"
android:gravity="top"
android:background="#DEDEDE" >
<RelativeLayout
android:id="@+id/background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_with_shadow"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/ttitle"
android:padding="5dp"
android:alpha="0.9"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/flCityBac"
android:layout_width="wrap_content"
android:layout_height="175dp"
android:layout_gravity="center_horizontal"
android:layout_weight="30" >
<ImageView
android:id="@+id/img_cityBac"
android:layout_width="300dp"
android:layout_height="175dp"
android:alpha="1"
android:background="@drawable/vancouvero2"
android:contentDescription="@null"
android:paddingLeft="0dp"
android:paddingRight="4dp"
/>
<TextView
android:id="@+id/text_Title"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:padding="0dp"
android:ellipsize="marquee"
android:gravity="center"
android:background="#E41B17"
android:alpha="0.85"
android:maxLines="1"
android:text="Vancouver, Canada"
android:textAllCaps="true"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="serif" />
</FrameLayout>
</RelativeLayout>
<TextView
android:id="@+id/ttitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:alpha="0.85"
android:ellipsize="marquee"
android:gravity="center"
android:maxLines="1"
android:text="Discover"
android:textAllCaps="true"
android:textColor="#E41B17"
android:textSize="28sp"
android:textStyle="bold"
android:typeface="serif" />
</RelativeLayout>
答案 0 :(得分:2)
而不是
getView().findViewById(R.id.flCityBac);
使用:
rootView.findViewById(R.id.flCityBac);
您无法使用getView,因为您的视图尚未创建。
希望它对你有所帮助! : - )
答案 1 :(得分:0)
public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_city, container, false);
FrameLayout icity = (FrameLayout)rootView.findViewById(R.id.flCityBac);//Use rootView here.
icity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
}
});
return rootView;
}
}