Android 为什么我的片段在点击按钮切换片段时不起作用?
我试图创建两个片段并将其插入片段容器,同时用两个按钮控制它们。没有错误,只是它不起作用 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa activity_main.xmlAndroid 为什么我的片段在点击按钮切换片段时不起作用?,android,android-studio,android-fragments,android-fragmentactivity,Android,Android Studio,Android Fragments,Android Fragmentactivity,我试图创建两个片段并将其插入片段容器,同时用两个按钮控制它们。没有错误,只是它不起作用 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa activity_main.xml <Button android:id="@+id
<Button
android:id="@+id/signinButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:onClick="ChangeFragment"
android:text="@string/sign_in"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.684"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/minikanikoTextView" />
<Button
android:id="@+id/signUpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="27dp"
android:onClick="ChangeFragment"
android:text="@string/sign_up"
app:layout_constraintBottom_toTopOf="@+id/fragment_container"
app:layout_constraintEnd_toEndOf="@+id/signinButton"
app:layout_constraintHorizontal_bias="0.479"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/minikanikoTextView"
app:layout_constraintVertical_bias="0.195" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="414dp"
android:layout_height="516dp"
android:layout_marginTop="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/signinButton"
app:layout_constraintVertical_bias="1.0">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
不要将视图作为对象进行比较 i、 不要
if(view==findViewById(R.id.signinButton)){
做
if(view.getId()==R.id.signinButton){
这意味着您正在将基本体int
与=
(而不是视图
对象)进行比较
当将一个对象(如视图
)与=
进行比较时,实际上是在比较内存中的对象引用是否与这两个对象相同(不会相同)
如果希望对象比较起作用,则需要使用.equals
即查看.equals(findViewById(R.id.signinButton))
但是,是否起作用取决于方法equals()
在视图.class中
因此您必须检查源代码,看看它是否正常工作。:)TLDR:比较原语ints更容易:)
参考文献:
(#7)
(#10)在
ChangeFragment
中放置断点如果什么都没有发生,我猜可能是该方法没有按预期调用,或者您的if语句的值从未计算为true
。您好,先生,我目前是android studio的新手。我如何在ChangeFragment中放置断点?@bigfabelly单击您想要的行号右侧在上面放一个断点。@MauroCurbelo谢谢你,先生…@Jack我已经放了断点,但什么也没发生我认为问题在于if语句我在javaYes上真的是新手,但这里两个引用是相同的,因为findViewById
在活动中为单个id返回相同的引用。当我更改if时(view==findViewById(R.id.signinButton)){
toif(view.getId()==R.id.signinButton){
我必须单击注册按钮才能工作……此外,开关部分没有工作,我还将从碎片容器中删除应用程序:布局约束垂直偏差
和应用程序:布局约束垂直偏差
package com.example.testonli;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ChangeFragment(View view){
Fragment fragment;
if(view == findViewById(R.id.signinButton)){
fragment = new SigninFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
}
if(view == findViewById(R.id.signUpButton)){
fragment = new SignupFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
}
}
}