setOnClickListener中的变量无法解析

时间:2012-09-23 16:49:00

标签: android onclicklistener

我到处寻找,但我找不到我要找的东西。

我正在尝试使用Chronometer内的Fragment为Android设置秒表应用程序,但我在某些按钮的setOnClickListener上收到错误。

它是括号“()”fx中的变量。 (mStartListener),我收到错误mStartListener cannot be resolved to a variable

请帮助我,我真的想进入android开发。谢谢

这是代码

b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- error
b = (Button) getView().findViewById(R.id.stop);
b.setOnClickListener(mStopListener); //--- error
b = (Button) getView().findViewById(R.id.reset);
b.setOnClickListener(mResetListener); //--- error

完整代码:

import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;

public class StopWatchFragment extends Fragment {

    Chronometer mChronometer;

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

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

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

        Button b;
        mChronometer = (Chronometer) getView().findViewById(R.id.chronometer);
        // Watch for button clicks.
        b = (Button) getView().findViewById(R.id.start);
        b.setOnClickListener(mStartListener); //--- error
        b = (Button) getView().findViewById(R.id.stop);
        b.setOnClickListener(mStopListener); //--- error
        b = (Button) getView().findViewById(R.id.reset);
        b.setOnClickListener(mResetListener); //--- error


        return view;


    View.OnClickListener mStartListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.start();
        }
    };
    View.OnClickListener mStopListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.stop();
        }
    };
    View.OnClickListener mResetListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.setBase(SystemClock.elapsedRealtime());
    }
    };
}
}

2 个答案:

答案 0 :(得分:1)

将每个Listener声明为类变量的位置移动:

public class StopWatchFragment extends Fragment {

    Chronometer mChronometer;
    View.OnClickListener mStartListener;
    ...

只是在尝试使用之前声明您的听众

Button b;
mChronometer = (Chronometer) getView().findViewById(R.id.chronometer);

View.OnClickListener mStartListener = new OnClickListener() {
    public void onClick(View v) {
        mChronometer.start();
    }
};
...

// Watch for button clicks.
b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- no more error

答案 1 :(得分:0)

您正在mStopListener方法中定义并初始化onCreateView AFTER 您尝试将它们设置为OnClickListener。

您应该将它们移动到方法的开头:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.stopwatch, container, false);
   View.OnClickListener mStartListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.start();
        }
    };
    View.OnClickListener mStopListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.stop();
        }
    };
    View.OnClickListener mResetListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.setBase(SystemClock.elapsedRealtime());
        }
    };
    Button b;
    mChronometer = (Chronometer) getView().findViewById(R.id.chronometer);
    // Watch for button clicks.
    b = (Button) getView().findViewById(R.id.start);
    b.setOnClickListener(mStartListener); //--- error
    b = (Button) getView().findViewById(R.id.stop);
    b.setOnClickListener(mStopListener); //--- error
    b = (Button) getView().findViewById(R.id.reset);
    b.setOnClickListener(mResetListener); //--- error

    return view;



}

或者,将它们作为全局变量放在方法之外。