gpt4 book ai didi

java - 使用TextView的onClick转到带有fragment的下一页

转载 作者:太空宇宙 更新时间:2023-11-04 13:39:20 25 4
gpt4 key购买 nike

    public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState)
{
myView = inflater.inflate(R.layout.first_layout, container, false);

tv.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {

tv = (TextView) myView.findViewById(R.id.textView);
Intent i = new Intent();
i.setClass(getActivity(), Second_Fragment.class);
startActivity(i);

}
});

return myView;
}

XML 代码:

    <ImageView
android:src="@drawable/processor"
android:layout_width="150dp"
android:layout_height="80dp"
android:id="@+id/imageView"
android:layout_weight="2" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Processor"
android:textSize="25dp"
android:id="@+id/textView2"
android:layout_weight="2"
android:clickable="true"/>
</TableRow>

错误代码:

     java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:57)
at android.os.Binder.execTransact(Binder.java:446)
07-13 04:47:09.009 1082-1092/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:64)
at android.os.Binder.execTransact(Binder.java:446)
07-13 04:47:09.236 1082-1092/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:71)
at android.os.Binder.execTransact(Binder.java:446)

最佳答案

首先:您没有启动 fragment 调用:

Intent i = new Intent();
i.setClass(getActivity(), Second_Fragment.class);
startActivity(i);

该方法的名称很明确:startActivity(i);因此,您应该学习如何启动 fragment 而不是启动 Activity !

第二:放置:

tv = (TextView) myView.findViewById(R.id.textView); 

在 setOnClickListener(...) 之前;

关于java - 使用TextView的onClick转到带有fragment的下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com