Создаю простейший сикбар и одно текстовое поле.
При изменении сикбара должно происходить изменение значения в поле. Но студия не компилирует код и вылетает в ошибку java.lang.NullPointerException. Поиски ошибки не увенчались успехом. Прошу помощи.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
SeekBar sb1;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv1 = (TextView)findViewById(R.id.tv1);
tv1.setText("");
SeekBar sb1 = (SeekBar)findViewById(R.id.sb1);
sb1.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tv1.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
Exeption
FATAL EXCEPTION: main Process: com.example.ivanvovk.linearlayout, PID: 21145
java.lang.NullPointerException
at com.example.ivanvovk.linearlayout.MainActivity.onProgressChanged(MainActivity.java:38)
at android.widget.SeekBar.onProgressRefresh(SeekBar.java:91)
at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:655)
at android.widget.ProgressBar.refreshProgress(ProgressBar.java:667)
at android.widget.ProgressBar.setProgress(ProgressBar.java:714)
at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:451)
at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:372)
at android.view.View.dispatchTouchEvent(View.java:7765)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
at android.view.View.dispatchPointerEvent(View.java:7945)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3961)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3840)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3532)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3589)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5609)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5589)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5560)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5689)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main
Ответ
У вас seekBar и textView объявлены два раза. Исправьте на это:
SeekBar sb1;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.tv1);
tv1.setText("");
sb1 = (SeekBar)findViewById(R.id.sb1);
sb1.setOnSeekBarChangeListener(this);
}
Комментариев нет:
Отправить комментарий