简体   繁体   English

如何膨胀xml布局以实现android seekbar的侦听器?

[英]How to inflate a xml layout in order to implement a listener for an android seekbar?

I have been spending hours trying to implement a seekbar listener for my android app. 我一直在花费数小时尝试为我的android应用程序实现seekbar侦听器。 Everytime I start the application it crashes. 每次我启动应用程序时,它都会崩溃。 I am trying to link the seekbar to a Threshold that can be manually changed over the seekbar. 我正在尝试将搜寻栏链接到可以在搜寻栏上手动更改的阈值。 This is my layout xml file for the seekbar: 这是我的搜索栏布局xml文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:minWidth="56dp">

<RelativeLayout
    android:layout_width="188dp"
    android:layout_height="match_parent">

    <SeekBar
        android:id="@+id/seekBar"
        style="@android:style/Widget.SeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_toStartOf="@+id/progressBar"
        android:max="127"
        android:progress="2" />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginEnd="60dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@+id/seekBar" />
</RelativeLayout>

My implementation looks as follows: 我的实现如下所示:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();
    getLayoutInflater().inflate(R.layout.actionbar_indeterminate_progress, null );
    setContentView(R.layout.actionbar_indeterminate_progress);
    final int step = 1;
    final int max = 0;
    final int min = -127;
    Threshold = (SeekBar) findViewById(R.id.seekBar);
    //Threshold.setMax( (max - min) / step );
    Threshold.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progressChangedValue;
        //@Override
        public void onProgressChanged(SeekBar Threshold, int progress,
                                      boolean fromUser)
        {
            // if progress = 13 -> value = 3 + (13 * 0.1) = 4.3
            double value = min + (progress * step);
            progressChangedValue = (int) value;
            //progressChangedValue = progress;
        }
        //@Override
        public void onStartTrackingTouch(SeekBar Threshold) {}

        //@Override
        public void onStopTrackingTouch(SeekBar Threshold) {
            mThreshold = progressChangedValue;
            Toast.makeText(DeviceScanActivity.this, "Threshold has been set to: " + progressChangedValue,
                    Toast.LENGTH_SHORT).show();
        }

    });
}

Does anyone have a hint what may cause the crash of the app? 是否有人暗示什么可能导致应用程序崩溃? And how I could solve the problem? 我该如何解决这个问题?

Thanks a lot! 非常感谢!

Sayus Sayus

You don't require to inflate a layout by yourself as setContentView() method does the same for you. 您不需要自己膨胀布局,因为setContentView()方法可以为您做同样的事情。

So remove getLayoutInflater().inflate(R.layout.actionbar_indeterminate_progress, null ); 因此,删除getLayoutInflater().inflate(R.layout.actionbar_indeterminate_progress, null ); from your code and it will stop crashing. 从您的代码,它将停止崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM