Java 调用活动和fragement之间的通信时出现空指针错误

Java 调用活动和fragement之间的通信时出现空指针错误,java,android,android-fragments,android-activity,Java,Android,Android Fragments,Android Activity,我一直在尝试从我的家庭活动调用一个方法,一旦在我的设置片段上单击一个按钮,该方法就会触发。这个按钮点击的结果应该调用greenstorso(),并在我的化身片段上交换一个图像。我目前收到一个空指针错误,请参阅所附的日志。我甚至不确定这样做是否正确,但我已经被困了几天,希望有人能帮助我。提前谢谢 我的家庭活动: public class Home extends AppCompatActivity implements SettingsFragment.Communicator { privat

我一直在尝试从我的家庭活动调用一个方法,一旦在我的设置片段上单击一个按钮,该方法就会触发。这个按钮点击的结果应该调用
greenstorso()
,并在我的化身片段上交换一个图像。我目前收到一个空指针错误,请参阅所附的日志。我甚至不确定这样做是否正确,但我已经被困了几天,希望有人能帮助我。提前谢谢

我的家庭活动:

public class Home extends AppCompatActivity implements SettingsFragment.Communicator {

private TextView name, email;
Button LogoutButton;
UserSessionManager sessionManager;
Button button;
ImageView Torso;

public void communicateWith(){
    AvatarFragment avatarFragment = (AvatarFragment)getSupportFragmentManager().findFragmentById(R.id.nav_avatar);
    avatarFragment.greenTorso();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    getWindow().setBackgroundDrawableResource(R.drawable.background2); // sets background to background2.png
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // hides keyboard on boot

    sessionManager = new UserSessionManager(getApplicationContext());

    TextView lblName = (TextView) findViewById(R.id.lblName);
    TextView lblEmail = (TextView) findViewById(R.id.lblEmail);

    BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
    bottomNav.setOnNavigationItemSelectedListener(navListener);
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
            new HomeFragment()).commit();

    if(sessionManager.checkLogin())
        finish();

    // get user data from session
    HashMap<String, String> user = sessionManager.getUserDetails();

    // get name
    String name = user.get(UserSessionManager.KEY_NAME);

    // get email
    String email = user.get(UserSessionManager.KEY_EMAIL);


    //lblName.setText(Html.fromHtml("Name: <b>" + name + "</b>"));
    lblEmail.setText(Html.fromHtml("Welcome: <b>" + email + "</b>"));

    LogoutButton=(Button)findViewById(R.id.LogoutButton);
    Toast.makeText(getApplicationContext(),
            "User Login Status: " + sessionManager.isUserLoggedIn(),
            Toast.LENGTH_LONG).show();

    LogoutButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // Clear the User session data
            // and redirect user to LoginActivity
            sessionManager.logoutUser();
        }
    });
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment selectedFragment = null;
                switch (menuItem.getItemId()) {
                    case R.id.nav_steps:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_avatar:
                        selectedFragment = new AvatarFragment();
                        break;
                    case R.id.nav_settings:
                        selectedFragment = new SettingsFragment();
                        break;

                }



 getSupportFragmentManager()
.beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
                return true;
            }
        };
}
我的化身片段:

public class AvatarFragment extends Fragment {
public ImageView IV;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_avatar, container, false);

   return rootView;


}

public void greenTorso()
{
    ImageView IV =(ImageView) getActivity().findViewById(R.id.Torso);
    IV.setBackgroundResource(R.drawable.torso2green);
}
}
头像布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:src="http://schemas.android.com/apk/res-auto"
android:background="@android:color/holo_orange_dark"
>

<ImageView
    android:layout_width="500dp"
    android:layout_height="530dp"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:layout_marginStart="30dp"
    android:layout_marginEnd="30dp"
    android:id="@+id/Torso"
    android:src="@drawable/torso2" >

</ImageView>
</RelativeLayout>

设置布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:orientation="vertical"
android:background="#D3D3D3"
android:weightSum="10"
tools:context=".Home">



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

<TextView
    android:layout_marginTop="100dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Shop"
    android:textSize="26sp"
    android:textStyle="bold"
    android:gravity="center"
    android:layout_marginBottom="10dp"/>


    <androidx.cardview.widget.CardView
        android:id="@+id/cardview1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_below="@+id/cardview"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="16dp"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="15dp"
        app:cardElevation="10dp">

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

        </LinearLayout>

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center_horizontal"
            android:paddingLeft="10dp"
            android:text="Green Torso"
            android:textColor="@color/cardview_dark_background"
            android:textSize="20sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/ShopGreenTorso"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/torso2green" />

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_gravity="bottom"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/register_button"
            android:shadowColor="@color/colorPrimary"
            android:text="10,000S Buy"
            android:textColor="@color/white">
        </Button>

    </androidx.cardview.widget.CardView>


导航化身的定义在哪里?是的,我认为这不是正确的ID。这是菜单项的ID。片段管理器无法将其与您的
化身片段关联。尝试查找
R.id.fragment\u容器
。更多信息请参见。不,我的意思是,这是您在代码中犯的原始错误。我不是指你的编辑;那很好,没关系。我认为你甚至不需要添加任何内容。我认为,如果您只需将
findFragmentById(R.id.nav_avatar)
切换到
findFragmentById(R.id.fragment_容器)
就可以了。但不是100%确定。让我们来。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:orientation="vertical"
android:background="#D3D3D3"
android:weightSum="10"
tools:context=".Home">



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

<TextView
    android:layout_marginTop="100dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Shop"
    android:textSize="26sp"
    android:textStyle="bold"
    android:gravity="center"
    android:layout_marginBottom="10dp"/>


    <androidx.cardview.widget.CardView
        android:id="@+id/cardview1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_below="@+id/cardview"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="16dp"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="15dp"
        app:cardElevation="10dp">

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

        </LinearLayout>

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center_horizontal"
            android:paddingLeft="10dp"
            android:text="Green Torso"
            android:textColor="@color/cardview_dark_background"
            android:textSize="20sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/ShopGreenTorso"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/torso2green" />

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_gravity="bottom"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/register_button"
            android:shadowColor="@color/colorPrimary"
            android:text="10,000S Buy"
            android:textColor="@color/white">
        </Button>

    </androidx.cardview.widget.CardView>
 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item
 android:id="@+id/nav_steps"
 android:icon="@drawable/ic_directions_walk_black_24dp"
 android:title="Steps" />
 <item
    android:id="@+id/nav_avatar"
    android:icon="@drawable/ic_face_black_24dp"
    android:title="Avatar" />
 <item
    android:id="@+id/nav_settings"
    android:icon="@drawable/ic_build_black_24dp"
    android:title="Settings" />

 </menu>