setOnSeekBarChangeListener为null

时间:2018-01-09 14:41:51

标签: android null seekbar android-seekbar

问题是尝试调用虚方法' void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar $ OnSeekBarChangeListener)'在空对象引用上

此外,它显示2行的错误,即fullBarM();在onCreate和fullBar.setOnSeekBarChangeListener(fullBarM中的新SeekBar.OnSeekBarChangeListener()。

我无法发现错误。 Ans建议?

public class MainActivity extends AppCompatActivity
{
private Context context;

private SeekBar fullBar;
private TextView fullBarText;
private int fullProgressValue = 0;
private TextView alarmText;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fullBarM();
}

public void fullBarM()
{
    //fullBar.setProgress(100);
    //fullBarText.setText("Covered : " + fullBar.getProgress() + " / " + fullBar.getMax());
    fullBar = (SeekBar) findViewById(R.id.full_Bar);
    fullBarText = (TextView) findViewById(R.id.full_Bar_Text);

    fullBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
            {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
                {
                    fullProgressValue = progress;
                    fullBarText.setText("Covered : " + progress + " / " + fullBar.getMax());

                    if (fullProgressValue <= percentage * 100)
                    {
                        alarmText.setText("Alarm");

                    }

                    else
                    {
                        alarmText.setText("Bos");
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                }
            }
    );
}

Activity_main

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/theLayout">

<SeekBar
    android:id="@+id/full_Bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="93dp"
    android:max="100"
    android:progressTint="@android:color/holo_blue_dark"
    android:thumbTint="@android:color/holo_blue_dark"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="16dp" />

<TextView
    android:id="@+id/full_Bar_Text"
    android:layout_width="105dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/full_Bar"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="26dp"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="59dp" />

</RelativeLayout>

0 个答案:

没有答案