2015-04-22 3 views
1

Я использую SeekBar изменить уровень непрозрачности на рисунок Но я получаю NullPointer исключения в строкахSeekBar исключение нулевого указателя

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

в следующем коде ..

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

Я попытался давая числовые аргументы в соответствии с этими строками, например seekTxt.setText (50); Но ошибка все еще остается тем же

Это мой 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> 
+0

Переместить 'seekDialog.setContentView (R.layout.opacity_chooser);' строка перед доступом к представлению из макета –

ответ

1

Первый набор контента Просмотр после инициализации SeekBar & 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); 
------------------- 
Смежные вопросы