如何解决如何解决无法通过onclick实现fragment跳转的问题?
程序没有报错,但是当我点击按钮跳转到片段时,程序直接崩溃,仍然没有报错。
这是 MainActicity 代码:
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.ic_launcher_background);
}
public void selectFragment(View view) {
Fragment fragment;
switch (view.getId()){
case R.id.button:
fragment = new testFragment();
break;
case R.id.button3:
fragment = new secTestFragment();
default:
throw new IllegalStateException("Unexpected value: " + view.getId());
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment2,fragment).commit();
}
}
这是我的主要活动布局。
<?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=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="267dp"
android:layout_marginBottom="160dp"
android:contentDescription="TODO"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="42dp"
android:layout_marginEnd="137dp"
android:layout_marginBottom="220dp"
android:onClick="selectFragment"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/fragment2"
app:layout_constraintVertical_bias="0.663" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="186dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="137dp"
android:text="textButton"
android:onClick="selectFragment"
app:layout_constraintBottom_toBottomOf="@+id/fragment2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmenttest.testFragment"
android:layout_width="408dp"
android:layout_height="411dp"
android:layout_marginTop="240dp"
android:layout_marginEnd="3dp"
android:layout_marginBottom="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是第一个片段页面布局。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".testFragment">
<ImageView
android:id="@+id/testImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:src="@tools:sample/avatars"
android:src="@drawable/burwood"/>
</FrameLayout>
这是第二个片段页面布局。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".secTestFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/fragmentTextView"
android:layout_width="match_parent"
android:layout_height="106dp"
android:gravity="center"
android:text="@string/hello_blank_fragment"
android:textSize="36sp"
android:textStyle="bold" />
</FrameLayout>
我希望能得到一些建议。谢谢!
解决方法
您需要调试您的应用程序,并且您可以在它崩溃之前设置一个断点。然后跳过崩溃,您应该在调试控制台或 logcat
输出中看到崩溃。如果由于某种原因它们不在那里,您可以在终端中运行 adb logcat
并在您运行应用程序时观察输出,它会在那里记录崩溃。
您可以找到有关命令行 logcat https://developer.android.com/studio/command-line/logcat
的更多信息版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。