2013-03-20 68 views
0

我的onclick方法添加到我的按钮,在fragment2.xml的Android的onClick方法,则不会调用

<Button 
     android:id="@+id/getTextButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Kliknij do fragment 1" 
     android:onClick="onClick" /> 

在Fragment2.java

public void onClick(View view){} 

,当我点击它的应用程序,然后定义这个方法,但崩溃?下面是什么

package com.example.fragments; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class Fragment2 extends Fragment { 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){ 
     Log.d("Fragment 2", "metoda"); 
     return inflater.inflate(R.layout.fragment2, container, false); 
    } 

    public void onClick(View view){} 

} 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#FFFF00" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Fragment 2" 
     android:textColor="#0000FF" 
     android:textSize="25sp"/> <!-- moze byc px dp sp --> 

    <Button 
     android:id="@+id/getTextButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Kliknij do fragment 1" 
     android:onClick="onClick" /> 

</LinearLayout> 
+0

你怎么知道这个不被调用? – Sajmon 2013-03-20 00:16:05

+0

这只对'活动'有效 – Geros 2013-03-20 00:18:22

回答

1

的onclick功能,将与在布局吹气的情况下被称为错全 XML和Java文件。

尝试将onClick函数放入你的Activity而不是你的Fragment,它应该可以工作。我之前有过similar problem

0

由于一些未知的原因,我也有类似的问题,我的onClick是在XML声明在某些设备上,并没有工作,参与过许多其他

我分配一个

OnClickListener 

到了同样的观点,在这个监听我手动调用

activity.onClick(view) 

,它的工作

在Android的SILLY错误!

0

在我的情况下,对活动具有“onClick”功能,我只需要清理项目...... xml on onClick调用后工作。