2015-08-23 67 views
0

其實我正在用滑動選項卡製作應用程序。下載了滑動標籤的代碼。它有3個片段和MainActivity,它擴展了FragmentActivity,並使用viewpager。滑動標籤工作正常。現在我正在嘗試在其中一個片段中添加一個按鈕,並在點擊該按鈕時顯示敬酒。下面是我在XML文件中添加的片段代碼:按鈕單擊不起作用的片段

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

    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Design Top Rated Screen" 
     android:textSize="20dp" 
     android:layout_centerInParent="true"/> 

    <Button 
     android:id="@+id/btn_frgmnt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Go to actvity" 
     android:onClick="navToActivity" 
     /> 
</RelativeLayout> 

而對於這個片段java文件裏面我做:

public class TopRatedFragment extends Fragment 
{ 

    Button btnNav; 

     Context context; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false); 
     btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt); 

     return rootView; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 

     super.onCreate(savedInstanceState); 
     //btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt); 
     context = getActivity().getApplicationContext(); 

    } 


    public void navToActivity(View v) 
    { 
     Toast.makeText(getActivity().getApplicationContext(), "Clicked inside fragment!", 2000).show(); 
    } 
} 

但其顯示的錯誤!我哪裏錯了。或者做點什麼來使按鈕點擊工作。它的這個片段類,我應該編寫按鈕點擊的代碼,對吧?

08-23 11:58:31.385: E/AndroidRuntime(3033): FATAL EXCEPTION: main 
    08-23 11:58:31.385: E/AndroidRuntime(3033): Process: info.androidhive.tabsswipe, PID: 3033 
    08-23 11:58:31.385: E/AndroidRuntime(3033): java.lang.IllegalStateException: Could not find a method navToActivity(View) in the activity class info.androidhive.tabsswipe.MainActivity for onClick handler on view class android.widget.Button with id 'btn_frgmnt' 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at android.view.View$1.onClick(View.java:3810) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at android.view.View.performClick(View.java:4438) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at android.view.View$PerformClick.run(View.java:18422) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at android.os.Handler.handleCallback(Handler.java:733) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at android.os.Handler.dispatchMessage(Handler.java:95) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at android.os.Looper.loop(Looper.java:136) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at android.app.ActivityThread.main(ActivityThread.java:5001) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at java.lang.reflect.Method.invokeNative(Native Method) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at java.lang.reflect.Method.invoke(Method.java:515) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at dalvik.system.NativeStart.main(Native Method) 
    08-23 11:58:31.385: E/AndroidRuntime(3033): Caused by: java.lang.NoSuchMethodException: navToActivity [class android.view.View] 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at java.lang.Class.getConstructorOrMethod(Class.java:472) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at java.lang.Class.getMethod(Class.java:857) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  at android.view.View$1.onClick(View.java:3803) 
    08-23 11:58:31.385: E/AndroidRuntime(3033):  ... 11 more 
+1

[onClick on fragment on called on Activity]的可能重複(http://stackoverflow.com/questions/7570575/onclick-inside-fragment-called-on-activity) – Simas

回答

0

在xml文件中聲明單擊事件的按鈕的回調函數必須位於您的活動類中。所以你必須在你的xml中聲明回調函數,否則你會得到一個異常。

要獲得click事件在你的片段,你需要調用「navToActivity的方法在你的片段explicitly.So在你的活動添加同樣的方法,並調用從按鈕單擊回調的片段法活動

public void navToActivity(View v) 
    { 
     topRatedFragment.navToActivity(v); 
    } 
+0

謝謝。但是,如果我沒有在xml文件中爲fragment定義方法,在哪裏初始化按鈕並編寫按鈕clickListener的代碼? –

+0

不要混淆。初始化部分將在您的片段中完成..但點擊回調將進入活動.. – Prasad

2

爲了捕捉按鈕事件片段使用的片段XML的查看或者乾脆註冊回調是這樣的:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false); 
     btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt); 

     btnNav.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Action to be performed on button click 
      } 
     }); 

     return rootView; 
    } 
0

注意,上面的XML,Android將查找我的onClick只在當前Activity中使用navToActivity()。如果您使用片段,這一點很重要,因爲即使使用片段添加上面的XML,Android也不會在用於添加XML的片段的.java文件中查找onClick方法。