2017-04-21 107 views
0

我一直花費數小時試圖爲我的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

回答

0

您不需要自行充氣的佈局作爲setContentView()方法做同樣爲您服務。

因此,從您的代碼中刪除getLayoutInflater().inflate(R.layout.actionbar_indeterminate_progress, null);,它將停止崩潰。

+0

@傑克雷:非常感謝您的回答。但是我的應用程序在刪除後仍然崩潰:getLayoutInflater()。inflate(R.layout.actionbar_indeterminate_progress,null); – Sayus

+0

可以在這裏發佈錯誤跟蹤,以便我可以更仔細地查看您的問題嗎? –