gpt4 book ai didi

java - NullPointerException - FrameLayout

转载 作者:行者123 更新时间:2023-11-29 20:58:38 25 4
gpt4 key购买 nike

我在加载 fragment Activity 时意外收到空指针异常错误。我指的是 FrameLayout 将用户定向到另一个 Activity (在单击按钮时)。

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)

下面是 Activity 代码:

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>

最佳答案

代替

getView().findViewById(R.id.flCityBac);

使用:

rootView.findViewById(R.id.flCityBac);

您不能使用 getView,因为您的 View 尚未创建。

希望对你有帮助! :-)

关于java - NullPointerException - FrameLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26847457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com