android-fragments 使用TextView的onClick转到包含片段的下一页[已关闭]

xj3cbfub  于 2022-11-13  发布在  Android
关注(0)|答案(3)|浏览(163)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

25天前关闭。
Improve this question

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)
ppcbkaq5

ppcbkaq51#

第一:你没有开始一个片段调用:

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

该方法的名称很清楚:启动活动(i);所以,你应该学习如何开始一个片段,而不是开始一个活动!
第二:把:

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

在setOnClickListener(...)之前;

x9ybnkn6

x9ybnkn62#

你不能通过startActivity()附加片段,假设你的Second_Fragment.class是一个片段。
从文档中,添加要进行分段的视图:

<FrameLayout 
     android:id="@+id/second_fragment"
     android:layout_width="wrap_content"   
     android:layout_height="match_parent" />

然后通过FragmentManager将其附加到onClick中的此视图:

// Create new fragment and transaction
Fragment secondFragment = new SecondFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Add the fragment to the second_fragment view with this fragment,
// and add the transaction to the back stack
transaction.add(R.id.second_fragment, secondFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
46qrfjad

46qrfjad3#

您可以在Activity和fragment中使用interface,每当您单击fragment中的textview时,就会通过interface向Activity发送数据,并从Activity更新fragment。为此,您必须使用onAttach()方法

相关问题