SeekBar空指针异常

时间:2015-04-22 11:56:50

标签: java android android-seekbar

我正在使用SeekBar来改变绘图中的不透明度级别但是我在行中得到了Nullpointer异常

public void onClick(View view){

if(view.getId()==R.id.opacity_btn){
        //launch opacity chooser

      final Dialog    seekDialog = new Dialog(this);
       final TextView  seekTxt =    (TextView)seekDialog.findViewById(R.id.opq_txt);
       final SeekBar seekOpq = (SeekBar)seekDialog.findViewById(R.id.seek);
        Toast.makeText(getApplicationContext(),
                "start.", Toast.LENGTH_SHORT).show();
       seekDialog.setTitle("Opacity level:");
       seekDialog.setContentView(R.layout.opacity_chooser);
       seekOpq.setMax(100);
       int currLevel = drawView.getPaintAlpha();
    seekTxt.setText(currLevel);
       seekOpq.setProgress(currLevel);
       seekOpq.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                seekTxt.setText(Integer.toString(progress)+" % ");
            }

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

        Button opqBtn = (Button)seekDialog.findViewById(R.id.opq_ok);
        opqBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                drawView.setPaintAlpha(seekOpq.getProgress());
              Toast.makeText(getApplicationContext(),
                        "end.", Toast.LENGTH_SHORT).show();
                seekDialog.dismiss();
            }
        });
 }

在以下代码中..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
    android:id="@+id/opq_txt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:text="100%"
    android:textStyle="bold" />
<SeekBar
    android:id="@+id/seek"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp" />
<Button
    android:id="@+id/opq_ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OK" />

</LinearLayout>

我已经尝试过将数字参数手动赋予这些行          seekTxt.setText(50); 但错误仍然是相同的

这是我的opacity_chooser.xml

%matplotlib inline
import matplotlib
matplotlib.style.use('ggplot')

1 个答案:

答案 0 :(得分:1)

初始化seekbar&amp;之后首先设置内容视图TextView中。

例如。

final Dialog    seekDialog = new Dialog(this);
seekDialog.setContentView(R.layout.opacity_chooser);
final TextView  seekTxt =    (TextView)seekDialog.findViewById(R.id.opq_txt);
final SeekBar seekOpq = (SeekBar)seekDialog.findViewById(R.id.seek);
Toast.makeText(getApplicationContext(),
            "start.", Toast.LENGTH_SHORT).show();
seekDialog.setTitle("Opacity level:");
seekOpq.setMax(100);
-------------------