2016-07-26 43 views
-1

沒有合適的方法,我有這樣的代碼: -從片段顯示舉杯 - 爲makeText

import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Toast; 
import android.widget.ToggleButton; 

public class Tab3 extends android.support.v4.app.Fragment { 
    private static final String ARG_SECTION_NUMBER = "section_number"; 
    private ToggleButton toggleButton1, toggleButton2; 
    private Button btnDisplay; 
    public void addListenerOnButton(View v) { 


     toggleButton1 = (ToggleButton) v.findViewById(R.id.toggleButton1); 
     toggleButton2 = (ToggleButton) v.findViewById(R.id.toggleButton2); 
     btnDisplay = (Button) v.findViewById(R.id.btnDisplay); 
     btnDisplay.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       StringBuffer result = new StringBuffer(); 
       result.append("toggleButton1 : ").append(
         toggleButton1.getText()); 
       result.append("\ntoggleButton2 : ").append(
         toggleButton2.getText()); 

       Toast.makeText(Tab3.this, result.toString(), 
         Toast.LENGTH_SHORT).show(); 

      } 

     }); 

    } 

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

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

它與 標籤頁一些消息切換按鈕,我有本條紅線: -

Toast.makeText(Tab3.this, result.toString(), 
         Toast.LENGTH_SHORT).show(); 

與此錯誤: -

Error:(32, 22) error: no suitable method found for makeText(Tab3,String,int) method Toast.makeText(Context,CharSequence,int) is not applicable (argument mismatch; Tab3 cannot be converted to Context) method Toast.makeText(Context,int,int) is not applicable (argument mismatch; Tab3 cannot be converted to Context)

任何人都可以幫我嗎?

回答

1

嘗試使用,而不是Tab3.this,使用此:getActivity()

+0

它的工作,我thaaanks很多 – mh9

2

makeText需要Context作爲第一個參數

Tab3Fragment。 A Fragment不是Context。取而代之的Tab3.this,使用Tab3.this.getContext()

+1

或者只是'的getContext()' –