无法解析方法onSectionAttached(int)

时间:2015-07-13 05:00:51

标签: android android-fragments

我在这里关注:https://youtu.be/ZdBe_tPOY-A?t=4m53s并且无法弄清楚为什么Android Studio会在第47行给我一个错误:OnSectionAttached(1);。它说'无法解决方法OnSectionAttached(int),虽然它在视频中运行得很好(he didn't have to define the method)。

这是关于文件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 个答案:

答案 0 :(得分:1)

您必须在onSectionAttached(int number)课程中创建MainActivity方法。例如:

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