2014-04-22 57 views
-2

我試圖在用戶觸摸某個單選按鈕進行選擇時,在我的片段之一上使用振動工具。我在public class Utility extends Activity之前使用過這個工具,並且它在我的MainActivity副本上使用它作爲片段。使用片段振動android

我將import android.os.Vibrator;導入到我的片段(稱爲Fragment_Bread)中,並將其聲明爲 private Fragment_Bread activity = this;

一切工作正常,除了現在當我救我的一切工具類有內部的錯誤它.getSystemService

我懸停,並得到以下錯誤「的方法getSystemService(字符串)是未定義的類型Fragment_Bread」

我跟隨型Fragment_Bread建議的修復「創建方法getSystemService(字符串)

這會在我的Fragment_Bread以下

public Vibrator getSystemService(String vibratorService) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

除了當我運行它時,應用程序崩潰,所有的都沒有錯誤。

我是在編程上的android和java新的,所以任何幫助將不勝感激。提前致謝。

Fragment_Bread,我寫道:

package com.example.fragmentsandwich; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Vibrator; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 

public class Fragment_Bread extends Fragment { 

    TextView section_label1; 
    Communicator comm; 
    Button submit_bread_button; 
    RadioGroup breadRadioGroup; 
    private Fragment_Bread activity = this; 

    // Bread 
    RadioButton white_button, wheat_button, rye_button, whole_grain_button, 
      flat_button; 
    RadioButton wrap_button, sour_dough_button, bagel_button; 

    public Fragment_Bread() { 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

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

     return rootView; 
    } 


    OnCheckedChangeListener listen = new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      Utility.vibrate(activity); 
      String bread = ""; 

      if (white_button.isChecked()) { 
       bread = "White"; 
      } 

      if (wheat_button.isChecked()) { 
       bread = "Wheat"; 
      } 

      if (rye_button.isChecked()) { 
       bread = "Rye"; 
      } 

      if (whole_grain_button.isChecked()) { 
       bread = "Whole grain"; 
      } 

      if (flat_button.isChecked()) { 
       bread = "Flat"; 
      } 

      if (wrap_button.isChecked()) { 
       bread = "Wrap"; 
      } 

      if (sour_dough_button.isChecked()) { 
       bread = "Sour dough"; 
      } 

      comm.update_bread(bread); 

     } 
    }; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     comm = (Communicator) getActivity(); 
     white_button = (RadioButton) getActivity().findViewById(
       R.id.white_button); 
     wheat_button = (RadioButton) getActivity().findViewById(
       R.id.wheat_button); 
     rye_button = (RadioButton) getActivity().findViewById(R.id.rye_button); 
     whole_grain_button = (RadioButton) getActivity().findViewById(
       R.id.whole_grain_button); 
     flat_button = (RadioButton) getActivity() 
       .findViewById(R.id.flat_button); 
     wrap_button = (RadioButton) getActivity() 
       .findViewById(R.id.wrap_button); 
     sour_dough_button = (RadioButton) getActivity().findViewById(
       R.id.sour_dough_button); 

     //white_button.setOnCheckedChangeListener(listen); 

     breadRadioGroup = (RadioGroup) getActivity().findViewById(R.id.breadRadioGroup); 
     for(int i = 0; i < breadRadioGroup.getChildCount(); i++) { 
      RadioButton radio_button = (RadioButton) breadRadioGroup.getChildAt(i); 
      radio_button.setOnCheckedChangeListener(listen); 
     } 

    } 

    public Vibrator getSystemService(String vibratorService) { 
     // TODO Auto-generated method stub 
     return null; 
    } 


} 

工具類,我寫道:

package com.example.fragmentsandwich; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Vibrator; 

public class Utility extends Fragment_Bread { 

public static void vibrate(Fragment_Bread activity) { 

    Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); 
    vibrator.vibrate(50); 

} //END VIBRATE 

} //END UTILITY 

回答

0

我假設Fragment_Bread從片段延伸。

Fragment類沒有getSystemService()方法。相反,你可以在你的Fragment上調用getActivity()並從那裏調用該方法。

public static void vibrate(Fragment_Bread fragment) { 
    Vibrator vibrator = (Vibrator) fragment.getActivity().getSystemService(Context.VIBRATOR_SERVICE); 
    vibrator.vibrate(50); 
} 
0

問題可能在這一行。

private Fragment_Bread activity = this; 

你應該使用

Utility.vibrate(getActivity()); 

代替。 快樂2幫助。