2016-08-06 1 views
0

내 조각 내에서 토스트를 만들려고합니다. 나는 인터넷에서 몇 시간을 검색해 왔고, 내가 만났던 것은 효과가없는 것 같다.Android : 탭 응용 프로그램의 토스트 및 탭 응용 프로그램의 다음 페이지 onclick

information.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        Toast toast = Toast.makeText(getActivity().getBaseContext(), "This information will not be published in the world wide web, but will be saved on your own device instead", Toast.LENGTH_SHORT); 
        toast.show(); 
        toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0); 
       } 
      });  

'getActivity은'빨간색입니다. 마우스를 올리면 "은 'getActivity()' '메서드를 해결할 수 없습니다. 나는 getActivity를 제거하고 오직 getBaseContext을 (사용) 또는 getApplicationContext()가는, 그것은 비 정적 방법은 정적 컨텍스트에서 참조 할 수 있다고합니다. 누구든지이 특정 토스트를 만드는 방법을 가르쳐 줄 수 있습니까?

Android Studio에서 표준 갤러리 탭 응용 프로그램을 사용했습니다. 또한

public class ApplicationFragments extends AppCompatActivity { 
     private SectionsPagerAdapter mSectionsPagerAdapter; 
     private ViewPager mViewPager; 

     //get widgets voor alle functies 
     private static View rootView; 

     //variable 
     private static int section; 
     //fragment1 
     static ImageButton information; 
     static int addOneSex; 
     static int addOneWeight; 
     static int addOneAge; 
     static ImageView plusSex; 
     static ImageView plusWeight; 
     static ImageView plusAge; 
     static TextView sex; 
     static TextView weight; 
     static TextView age; 

     //fragment2 
     static int addOneHour; 
     static int addOneBeer; 
     static int addOneWine; 
     static int addOneShot; 
     static ImageView plusBeers; 
     static ImageView plusWines; 
     static ImageView plusShots; 
     static TextView plusHours; 
     static TextView amountOfHours; 
     static TextView amountOfBeers; 
     static TextView amountOfWines; 
     static TextView amountOfShots; 

     //fragment3 

     //all fragments 
     static Button btnNext; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      //verander animatie 
      overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.fade_out); 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_information); 

      // 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); 
     } 

     public static class PlaceholderFragment extends Fragment { 
      private static final String ARG_SECTION_NUMBER = "section_number"; 

      public static PlaceholderFragment newInstance(int sectionNumber) { 
       PlaceholderFragment fragment = new PlaceholderFragment(); 
       Bundle args = new Bundle(); 
       args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
       fragment.setArguments(args); 
       return fragment; 
      } 

      //bij het creëren van een tab 
      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) { 
       rootView = inflater.inflate(R.layout.fragment_information, container, false); 

       //set xml 
       section = getArguments().getInt(ARG_SECTION_NUMBER); 
       if (section == 1) { 
        rootView = inflater.inflate(R.layout.fragment_information, container, false); 
        GenerateFragment1(); 
       } 
       if (section == 2) { 
        rootView = inflater.inflate(R.layout.fragment_drinks, container, false); 
        GenerateFragment2(); 
       } 
       if (section == 3) { 
        rootView = inflater.inflate(R.layout.fragment_calculate, container, false); 
        GenerateFragment3(); 
       } 
       return rootView; 
      } 
     } 

     //geef de juiste pagina terug 
     public class SectionsPagerAdapter extends FragmentPagerAdapter { 
      public SectionsPagerAdapter(FragmentManager fm) { 
       super(fm); 
      } 

      @Override 
      public Fragment getItem(int position) { 
       return PlaceholderFragment.newInstance(position + 1); 
      } 

      @Override 
      public int getCount() { 
       //hoeveelheid pagina's 
       return 3; 
      } 

      @Override 
      public CharSequence getPageTitle(int position) { 
       switch (position) { 
        case 0: 
         return "SECTION 1"; 
        case 1: 
         return "SECTION 2"; 
        case 2: 
         return "SECTION 3"; 
       } 
       return null; 
      } 
     } 

     //fragment 1 
     public static void GenerateFragment1(){ 
      information = (ImageButton) rootView.findViewById(R.id.imgBtnInfo); 
      sex = (TextView) rootView.findViewById(R.id.etSex); 
      weight = (TextView) rootView.findViewById(R.id.etWeight); 
      age = (TextView) rootView.findViewById(R.id.etAge); 
      plusSex = (ImageView) rootView.findViewById(R.id.btnSex); 
      plusWeight = (ImageView) rootView.findViewById(R.id.btnWeight); 
      plusAge = (ImageView) rootView.findViewById(R.id.btnAge); 
      btnNext = (Button) rootView.findViewById(R.id.btnNext); 

      plusSex.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        addOneSex = Integer.parseInt(sex.getText().toString()); 
        addOneSex++; 
        sex.setText(Integer.toString(addOneSex)); 
       } 
      }); 

      plusWeight.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        addOneWeight = Integer.parseInt(weight.getText().toString()); 
        addOneWeight++; 
        weight.setText(Integer.toString(addOneWeight)); 
       } 
      }); 

      plusAge.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        addOneAge = Integer.parseInt(age.getText().toString()); 
        addOneAge++; 
        age.setText(Integer.toString(addOneAge)); 
       } 
      }); 


      information.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        Toast toast = Toast.makeText(getActivity().getBaseContext(), "This information will not be published in the world wide web, but will be saved on your own device instead", Toast.LENGTH_SHORT); 
        toast.show(); 
        toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0); 
       } 
      }); 

      btnNext.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        //to the next fragment 
       } 
      }); 
     } 

가, 내가 응용 프로그램이 buttonclick (btnNext)에 다음 페이지로 이동 할 수있는 방법 궁금 : 여기 내 코드 (당신이 맨 아래에 그것을 발견 할 것이다)입니다.

시간과 도움에 감사드립니다!

+0

나는 모든 변수를 정적으로 만드는 의도가 무엇인지 알고있을 수 있습니까 ?? 나는 왜 당신의 버튼과 이미지 뷰가 정적이라는 것을 의미합니까 ?? – Moulesh

+0

정적 앞에 놓지 않으면 '정적 필드가 아닌 ... 정적 컨텍스트에서 참조 할 수 없습니다'라는 메시지가 표시됩니다. 나는 꽤 새롭다. 더 좋은 방법이 있다면, 쏴라! –

+0

글쎄 drawable 자원을 정적으로 만드는 것은 지옥처럼 휴대 전화 메모리를 찢을 것이고, 메모리 부족 오류 및 앱 충돌의 결과를 초래할 수 있습니다 ... 정확히 내가 무엇을하려고하는지 알 수 있습니다. 접근 방법은 무엇입니까 ?? 당신은 단 하나 활동이 있고 나머지는 조각입니까 ?? 또는 여러 가지 활동이 있습니까 ?? – Moulesh

답변

0

어딘가에 액티비티에 저장된 프래그먼트 목록이나 프래그먼트 관리자가 있습니까? 그렇다면 목록에서 다음 조각을 가져 와서 조각 관리자로 전환하면됩니다. 토스트 getActivity() 문제에 관해서 : 올바르게 이해하면, ApplicationFragments가 주 활동입니까? 그렇다면 getActivity() 대신 ApplicationFragments.this을 호출하는 것이 어떻습니까?

+0

은 정적 컨텍스트에서 참조 할 수 없습니다. 이 탭이 달린 활동 전에 메뉴를 가지고 있기 때문에 실제로 나의 주된 활동이 아닙니다. –

+0

ApplicationFragments.this는 무엇을 의미합니까? 또한 거의 모든 필드를 비공개 및 비 정적으로 변경하는 것이 좋습니다. 그것은 잠재적 인 문제를 많이 없애 버릴 것입니다. –

+0

또한 튜토리얼/가이드를 따라 가셨습니까? –

관련 문제