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

问题描述:

我一直花费数小时试图为我的android应用程序实现seekbar侦听器。每次我开始应用程序崩溃。 我试图将seekbar链接到可以在搜索栏上手动更改的阈值。这是搜索条我的布局xml文件:如何膨胀xml布局以实现android seekbar的侦听器?

<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> 

我的实现如下所示:

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(); 
     } 

    }); 
} 

有没有人有一个提示什么可能会导致应用程序崩溃?我该如何解决这个问题?

非常感谢!

Sayus

您不需要自行充气的布局作为setContentView()方法做同样为您服务。

因此,从您的代码中删除getLayoutInflater().inflate(R.layout.actionbar_indeterminate_progress, null);,它将停止崩溃。

+0

@杰克雷:非常感谢您的回答。但是我的应用程序在删除后仍然崩溃:getLayoutInflater()。inflate(R.layout.actionbar_indeterminate_progress,null); – Sayus

+0

可以在这里发布错误跟踪,以便我可以更仔细地查看您的问题吗? –