即使在OnCreateView上充气后,视图仍然为null

时间:2018-06-05 19:27:40

标签: android android-fragments

我已经在同样的撞车中挣扎了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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您的视图未初始化且仍为null。在“if语句”之前,按如下所示初始化视图:

questionRepo.save()

其他一切都很好