2011-06-03 46 views
2

我试图使用按钮移动seekbar的位置。基本上我有一个从0到100的seekbar,并且我有按钮设置为任意值(40,50,60等)。当我尝试通过按钮设置seekbar的进度时,我得到一个错误..我已经初始化了onCreate()方法中的seekBar。使用不同的按钮更改SeekBar的拇指位置

SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1); 
    currentProgress = 40; 
    seekBar.setMax(100); 
    seekBar.setProgress(currentProgress); 
    button40.setOnClickListener(button40Listener); 

但是,当使用下面,它崩溃。

private OnClickListener button40Listener = new OnClickListener() { 
     public void onClick(View v) { 
      currentProgress = 40; 
      seekBar.setProgress(currentProgress); 
     } 
    } 

这似乎是直截了当的。有任何想法吗?

+0

当然,现在我已经贴了问题,我似乎想到了什么,我解决了这个问题!我结束了另一个 SeekBar seekBar =(SeekBar)findViewById(R.id.seekBar1);在onClick方法中使用 。 – r00 2011-06-03 02:09:35

+0

只是一个范围错误,使其可以看到你的整个活动,而不是在你的一个方法中分配它。我有时会这么做,并且会在追尾时间过几分钟! – 2011-09-28 05:13:33

回答

3

你应该不需要再搭配一个Seekbar。最初的一个应该没问题。没有异常消息和堆栈跟踪,我不知道是什么导致崩溃。但是,我只是编写了一个示例,并按照您的预期工作。也许通过查看我的例子,您可以确定您的问题。

SeekbarTest.java:

package com.example.seekbartest; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.SeekBar; 

public class SeekbarTest extends Activity { 
/** Called when the activity is first created. */ 
private SeekBar seekBar; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    seekBar = (SeekBar)findViewById(R.id.seekBar1); 

    Button fortyPctButton = (Button)findViewById(R.id.buttonFortyPct); 
    fortyPctButton.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      seekBar.setProgress(40); 
     } 
    }); 

    Button sixtyPctButton = (Button)findViewById(R.id.buttonSixtyPct); 
    sixtyPctButton.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      seekBar.setProgress(60); 
     } 
    }); 

    Button eightyPctButton = (Button)findViewById(R.id.buttonEightyPct); 
    eightyPctButton.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      seekBar.setProgress(80); 
     } 
    }); 
    } 
} 

这里是它引用了布局的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<TextView 
    android:layout_width="fill_parent" 
    android:text="@string/hello" 
    android:id="@+id/textView1" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true"/> 

<SeekBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/seekBar1" 
    android:layout_below="@+id/textView1" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_alignRight="@+id/textView1"/> 

<Button 
    android:layout_width="wrap_content" 
    android:text="40%" 
    android:id="@+id/buttonFortyPct" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/seekBar1" 
    android:layout_alignLeft="@+id/seekBar1"/> 

<Button 
    android:layout_width="wrap_content" 
    android:text="60%" 
    android:id="@+id/buttonSixtyPct" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/buttonFortyPct" 
    android:layout_alignTop="@+id/buttonFortyPct" 
    android:layout_alignBottom="@+id/buttonFortyPct"/> 

<Button 
    android:layout_width="wrap_content" 
    android:text="80%" 
    android:id="@+id/buttonEightyPct" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/buttonSixtyPct" 
    android:layout_alignTop="@+id/buttonSixtyPct" 
    android:layout_alignBottom="@+id/buttonSixtyPct"/> 
</RelativeLayout> 

只需创建一个新的Android应用和替换生成的代码+布局上面的例子。它应该适合你。

祝你好运, 克雷格