使用片段振动android

时间:2014-04-22 05:03:01

标签: java android android-fragments

当我触摸其中一个单选按钮进行选择时,我试图在我的一个片段上使用振动工具。我在public class Utility extends Activity之前使用过这个工具,它在我的MainActivity恶习片段上使用它。

我将import android.os.Vibrator;导入我的片段(称为Fragment_Bread)并将其声明为 private Fragment_Bread activity = this;

一切正常,除非现在保存我的Utility类中有错误.getSystemService

我将鼠标悬停在它上面并收到以下错误"方法getSystemService(String)未定义类型Fragment_Bread"

我遵循建议的修复"在Fragment_Bread类型中创建方法getSystemService(String)

这会在我的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

2 个答案:

答案 0 :(得分:0)

我假设Fragment_Bread从Fragment扩展。

Fragment类没有getSystemService()方法。相反,您可以在Fragment上调用getActivity()并从那里调用该方法。

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

答案 1 :(得分:0)

问题可能出在这一行。

private Fragment_Bread activity = this;

你应该使用

Utility.vibrate(getActivity());

代替。 快乐2帮助。