简体   繁体   中英

SeekBar null pointer exception

I am using SeekBar to change the opacity level in drawing But I am getting Nullpointer exception in the lines

  seekOpq.setMax(100);
      and
    seekTxt.setText(currLevel);

in the following code..

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

I have tried giving numeric arguments manualy to these lines like seekTxt.setText(50); But the error still remains the same

This is my opacity_chooser.xml

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

First set Content View after initialize seekbar & textview.

eg.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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