使用TextView的onClick转到包含片段

时间:2015-07-12 22:02:56

标签: java android android-fragments onclicklistener

这是这样,但现在遵循代码块:

    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)

3 个答案:

答案 0 :(得分:0)

假设您的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();

答案 1 :(得分:0)

首先:你没有开​​始调用片段:

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

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

第二:把:

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

在setOnClickListener(...);

之前

答案 2 :(得分:0)

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