Просмотр по-прежнему нулевой, даже после надувания OnCreateView - PullRequest
0 голосов
/ 05 июня 2018

Я боролся с тем же сбоем уже 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>

1 Ответ

0 голосов
/ 05 июня 2018

Ваше представление не инициализировано, и оно все еще нулевое.Перед вашим «оператором if» инициализируйте представление следующим образом:

view = (View) inflater.inflate(R.layout.your_layout_file, container, false);

все остальное в порядке

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...