使用 TextView 的 onClick 转到带有片段的下一页


    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)

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

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

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

第二步:放入:

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

之前setOnClickListener(…),

你不能通过startActivity()附加一个片段,假设你的Second_Fragment.class是一个片段。看看如何创建片段http://developer.android.com/guide/components/fragments.html

从文档中,添加您想要片段去的视图:

<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();

你可以在Activity和fragment中使用interface每当您单击片段中的textview时,JST通过接口将数据发送到活动,并从活动中更新片段为此,您必须使用onAttach()方法

最新更新