Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试调用虚拟方法'';尝试从.java文件调用对象时,在空对象引用上_Java_Android_Android Fragments - Fatal编程技术网

尝试调用虚拟方法'';尝试从.java文件调用对象时,在空对象引用上

尝试调用虚拟方法'';尝试从.java文件调用对象时,在空对象引用上,java,android,android-fragments,Java,Android,Android Fragments,您好,我在尝试将一个变量从fragments.XML文件导入到相应的java文件时遇到了这个问题。据我所知,无法看到它有一个问题,它需要类似LayoutInflater.inflate()的东西才能让片段看到它,但我不确定,因为我是新手 如果有人能帮忙,那就太好了!我试图让java文件看到“loadContainer”和“loadMore”,因为我试图编辑它们的可见性 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example

您好,我在尝试将一个变量从fragments.XML文件导入到相应的java文件时遇到了这个问题。据我所知,无法看到它有一个问题,它需要类似LayoutInflater.inflate()的东西才能让片段看到它,但我不确定,因为我是新手

如果有人能帮忙,那就太好了!我试图让java文件看到“loadContainer”和“loadMore”,因为我试图编辑它们的可见性

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.tkgapp, PID: 32529
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
        at com.example.tkgapp.ui.trips.tripsFragment.onCreate(tripsFragment.java:46)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
        at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
my.java文件

package com.example.tkgapp.ui.trips;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.tkgapp.R;

public class tripsFragment extends Fragment {

    private tripsViewModel mViewModel;


    
    public static tripsFragment newInstance() {
        return new tripsFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_trips, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(tripsViewModel.class);
        // TODO: Use the ViewModel
    }

    ConstraintLayout loadContainer1;
    TextView loadMore1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadContainer1 = (ConstraintLayout) root.findViewById(R.id.loadContainer);

        loadMore1 = (TextView) getView().findViewById(R.id.loadMore);
        loadMore1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (loadMore1.getText().toString().equalsIgnoreCase("More..."))
                {
                    loadContainer1.setVisibility(View.VISIBLE);
                    loadMore1.setText("Showless");
                }
                else
                {
                    loadContainer1.setVisibility(View.INVISIBLE);
                    loadMore1.setText("More...");
                }
            }
        });

    }


}
XML文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.trips.tripsFragment">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="80dp"
        android:layout_marginRight="80dp"
        android:background="@drawable/button_round_corners"
        android:text="Contact Us"
        app:backgroundTint="@android:color/holo_blue_light"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="Need help?"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_bias="0.809"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button2"
        app:layout_constraintVertical_bias="0.482" />

    <ScrollView
        android:layout_width="411dp"
        android:layout_height="659dp"
        android:layout_marginTop="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/booking1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/section_round_corners">

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="16dp"
                    android:text="Booking Reference (Upcoming):"
                    android:textColor="@color/Black"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/tripInfo1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="5dp"
                    android:layout_marginLeft="5dp"
                    android:text="ABC123"
                    app:layout_constraintBottom_toBottomOf="@+id/textView3"
                    app:layout_constraintStart_toEndOf="@+id/textView3"
                    app:layout_constraintTop_toTopOf="@+id/textView3" />

                <ImageView
                    android:id="@+id/imageView6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="24dp"
                    android:src="@drawable/promotion4"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/textView3" />

                <TextView
                    android:id="@+id/triptext1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="20dp"
                    android:text="Start Date:"
                    android:textColor="@color/Black"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/imageView6" />

                <TextView
                    android:id="@+id/triptext2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Duration:"
                    android:textColor="@color/Black"
                    app:layout_constraintStart_toStartOf="@+id/triptext1"
                    app:layout_constraintTop_toBottomOf="@+id/triptext1" />

                <TextView
                    android:id="@+id/tripInfo2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="5dp"
                    android:layout_marginLeft="5dp"
                    android:text="01/01/2020"
                    app:layout_constraintStart_toEndOf="@+id/triptext1"
                    app:layout_constraintTop_toTopOf="@+id/triptext1" />

                <TextView
                    android:id="@+id/tripInfo3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="5dp"
                    android:layout_marginLeft="5dp"
                    android:text="20 Days"
                    app:layout_constraintStart_toEndOf="@+id/triptext1"
                    app:layout_constraintTop_toBottomOf="@+id/tripInfo2" />

                <TextView
                    android:id="@+id/triptext3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="124dp"
                    android:layout_marginLeft="124dp"
                    android:layout_marginTop="20dp"
                    android:text="End Date:"
                    android:textColor="@color/Black"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toEndOf="@+id/triptext1"
                    app:layout_constraintTop_toBottomOf="@+id/imageView6" />

                <TextView
                    android:id="@+id/triptext4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Destination"
                    android:textColor="@color/Black"
                    app:layout_constraintStart_toStartOf="@+id/triptext3"
                    app:layout_constraintTop_toBottomOf="@+id/triptext3" />

                <TextView
                    android:id="@+id/tripInfo4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="02/02/20202"
                    app:layout_constraintStart_toStartOf="@+id/tripInfo5"
                    app:layout_constraintTop_toTopOf="@+id/triptext3" />

                <TextView
                    android:id="@+id/tripInfo5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginLeft="8dp"
                    android:text="Europe"
                    app:layout_constraintStart_toEndOf="@+id/triptext4"
                    app:layout_constraintTop_toBottomOf="@+id/triptext3" />

                <TextView
                    android:id="@+id/loadMore"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25dp"
                    android:layout_marginBottom="15dp"
                    android:text="@string/more"
                    android:textColor="@color/DarkBlue"
                    android:visibility="visible"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/triptext4" />

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/loadContainer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp"
                    app:layout_constraintBottom_toTopOf="@+id/loadMore"
                    app:layout_constraintTop_toBottomOf="@+id/triptext4"
                    app:layout_constraintVertical_bias="0.0"
                    tools:layout_editor_absoluteX="0dp">

                    <TextView
                        android:id="@+id/tripInfo6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginLeft="16dp"
                        android:layout_marginTop="4dp"
                        android:text="Trip Title:"
                        android:textColor="@color/Black"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/tripInfo8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="5dp"
                        android:layout_marginLeft="5dp"
                        android:text="15 day Vietnam and Cambodia River Tour"
                        app:layout_constraintStart_toEndOf="@+id/tripInfo6"
                        app:layout_constraintTop_toTopOf="@+id/tripInfo6" />

                    <TextView
                        android:id="@+id/tripInfo9"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="5dp"
                        android:layout_marginRight="5dp"
                        android:text="@string/desc"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="@+id/tripInfo8"
                        app:layout_constraintTop_toTopOf="@+id/tripInfo7" />

                    <TextView
                        android:id="@+id/tripInfo7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Desc:"
                        android:textColor="@color/Black"
                        tools:layout_editor_absoluteX="16dp"
                        tools:layout_editor_absoluteY="36dp" />

                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.constraintlayout.widget.ConstraintLayout>
        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

从片段中删除此onCreate,并将代码放入onViewCreated中,然后使用'view'参数按id查找视图。如下所示:

package com.example.tkgapp.ui.trips;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.tkgapp.R;

public class tripsFragment extends Fragment {

    private tripsViewModel mViewModel;


    
    public static tripsFragment newInstance() {
        return new tripsFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_trips, container, false);
    }

   

    ConstraintLayout loadContainer1;
    TextView loadMore1;
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState)
        mViewModel = new ViewModelProvider(this).get(tripsViewModel.class);

        loadContainer1 = (ConstraintLayout) view.findViewById(R.id.loadContainer);

        loadMore1 = (TextView) view.findViewById(R.id.loadMore);
        loadMore1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (loadMore1.getText().toString().equalsIgnoreCase("More..."))
                {
                    loadContainer1.setVisibility(View.VISIBLE);
                    loadMore1.setText("Showless");
                }
                else
                {
                    loadContainer1.setVisibility(View.INVISIBLE);
                    loadMore1.setText("More...");
                }
            }
        });

    }


}


看,上面是活动和片段生命周期。这样您就知道调用了哪些函数及其顺序。

什么是
?这在任何地方都没有定义。谢谢!这解决了我的问题。你能解释一下为什么会有帮助吗?是的,你没有使用片段生命周期的功能,所以在OnCreate中,视图还没有创建。在onCreate之后,调用OnCreateView,然后您的视图膨胀。我将用Android view lifecycle的图像更新评论。