android - 即使在 OnCreateView 上充气后,视图仍然为空
问题描述
我已经在同样的崩溃中挣扎了 4 个小时,但我仍然无法弄清楚它的原因。任何善良的灵魂可以启发我吗?我收到此错误
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.view.View android.view.View.findViewById(int)'
它开始加载我的 textview lbl 的那一刻它只是崩溃并给我这个错误
下面是我的代码:
public class BookDoc extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.activity_main, container, false);
} catch (InflateException e) {
}
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Typeface type = Typeface.createFromAsset(getActivity().getAssets(),"fonts/verdana.ttf");
TextView lbl = (TextView) view.findViewById(R.id.textViewa);
return view
}
这是我的 xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f6f5"
>
<ScrollView
android:id="@+id/sv123"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:orientation="vertical"
android:background="#cc7781"
android:visibility="gone"
>
<TextView
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:id="@+id/tba"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:gravity="center"
android:text="View Existing Booking"
android:textSize="20dp" />
<TextView
android:id="@+id/tbb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:gravity="center"
android:text="You have already made a booking."
android:textSize="15dp" />
<TextView
android:layout_marginBottom="20dp"
android:id="@+id/tbc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:gravity="center"
android:text="Would you like to view it?"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_below="@+id/top"
android:id="@+id/top2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:layout_below="@+id/top2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#f5f6f5"
>
<TextView
android:id="@+id/textViewa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#7D7D7D"
android:text="SELECT A NEARBY CLINIC" />
<View
android:layout_marginTop="5dp"
android:id="@+id/textView2"
android:layout_below="@+id/textViewa"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<ListView
android:layout_marginTop="5dp"
android:layout_below="@+id/textView2"
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/textViewff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/listView1"
android:textColor="#e94256"
android:textSize="20dp"
android:text="View All Clinics" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
解决方案
您的视图未初始化,它仍然为空。在您的“if 语句”之前,初始化视图如下:
view = (View) inflater.inflate(R.layout.your_layout_file, container, false);
其他一切都很好
推荐阅读
- python - 使用 Pytest 夹具和 Flask 测试 LiveServerTestCase
- swift - 无法从应用程序扩展访问我的“CKContainer”中的任何数据 - CloudKit,Swift
- tensorflow - Debug broken Tensorflow-gpu installation with Conda (1.14 vs 2.3), Ubuntu 18.04
- linq - 一个 LinqPad 查询中的多个数据源以比较 Dynamics 365 和 Dynamics NAV 数据
- nativescript - 使用 nativescript-drop-down 插件时出错
- javascript - 服务器发送事件意外停止
- typescript - 确保字符串联合具有特定字符串文字的元素的映射类型
- html - 如果我在第一个上使用 mask-image,我如何将第二个 div 重叠在第一个上方?
- node.js - 与远程服务器进行 SSL 握手期间的 httpd 代理错误
- python - 如何将 python pygame 函数从字符串更改为 python 中的函数?