2015-07-13 107 views
0

我沿着這裏:https://youtu.be/ZdBe_tPOY-A?t=4m53s,並不能找出爲什麼Android Studio給我一個錯誤在第47行:OnSectionAttached(1);。它說'無法解析方法OnSectionAttached(int),而它在視頻中工作得很好(he didn't have to define the method)。無法解決方法onSectionAttached(int)

這裏的文件AboutFragment.java:

package trade.android.example.com.stitch; 


import android.app.Activity; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* Created by FiringBlanks on 7/12/2015. 
*/ 

public class AboutFragment extends Fragment { 

public static AboutFragment newInstance(){ 

    AboutFragment fragment = new AboutFragment(); 
    return fragment; 
} 

public AboutFragment(){ 

} 


@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_about, container, false); 
    return rootView; 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 

    ((MainActivity) activity).onSectionAttached(1); 

} 
} 

回答

1

你必須創建MainActivity類中onSectionAttached(int number)方法。例如:

public void onSectionAttached(int number) { 
    switch (number) { 
     case 1: 
      // Your code 
      break; 
     case 2: 
      // your code 
      break; 
    } 
} 
+0

我只是快速轉發視頻到4:34,並看到他定義這種方法。我應該在發佈哈哈之前完成視頻。謝謝! – FiringBlanks

相關問題