当我放置的方法是活动时,如何从片段活动中调用方法?

时间:2018-10-22 06:54:19

标签: android

当我放置的方法是活动时,如何从片段活动中调用方法?

public class PosMainActivity extends AppCompatActivity {
      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_trx);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
         response_message = extras.getString("RESPONSE_MESSAGE");

    }

    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    mViewPager.addOnPageChangeListener(new 
   TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new 
       TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
     }

   public  void parseJsonResponse(String jsonResponse){
// I parses and adds the value to the text view or button here
         try {
            JSONObject ResponseHeaderObject = new JSONObject(responseData);
            String UIHeader = ResponseHeaderObject.getString("HEADER");
            //Set Header Display
            headerTextView =(TextView) findViewById(R.id.headerTextView);
            headerTextView.setText(UIHeader);

        }catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 }

     /**
     * A placeholder fragment containing a simple view.
     */
       public static class PlaceholderFragment extends Fragment {
    @Override
    public void onViewCreated(View v, Bundle savedInstanceState) {
         super.onViewCreated(v, savedInstanceState);
         parseJsonResponse(response_message);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

        //Switch to different layout accordingly
        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1: {
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
                break;
            }
            case 2: {
                rootView = inflater.inflate(R.layout.fragment_receipt, container, false);
                break;
            }


        }
        return rootView;
      }
   }
}

当我将parseJsonResponse放在onViewCreated()方法中时,出现以下错误:

  

非静态字段无法从静态上下文android

引用

parseJsonResponse()在我的Activity类中,因此无法将其更改为静态方法。

在这种情况下我该怎么办?

1 个答案:

答案 0 :(得分:0)

解决方案:

从片段调用Activity的方法

 ((YourActivityName)getActivity()).addUserLineInfoFragment();

警告::这可能会导致某些东西泄漏。因此,

从“活动”中调用片段的方法

1。创建界面

 public interface OnButtonListener
    {
        void onButtonClick();
    }

2。在活动中初始化

 protected OnButtonListener onActionButtonListener;

    public void setOnButtonListener(OnButtonListener actionButtonListener)
    {
        this.onActionButtonListener = actionButtonListener;
    }

3。在“活动”中,在需要执行此操作时单击事件

this.onActionButtonListener.onButtonClick();

4。在片段中实现监听器(OnButtonListener)

 @Override
    public void onButtonClick(){}

5。片段onCreateView

((YourActivityName) getActivity()).setOnButtonListener(this);

来源: Refer This