Search code examples
androidseekbar

SeekBar Stops Activity | What is the error?


I design and implement a seek bar which incements by 50. But if i run app it is crashing. There is no syntax error (Eclipse does not notice me about this) What is the problem?

package com.example.hello;
import com.example.hello.R.layout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class NewPlan extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_plan);

    SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
    seekBar.setProgress(0); //this is line 19
    seekBar.incrementProgressBy(50);
    seekBar.setMax(5000);


    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
        TextView seekBarValue = (TextView)findViewById(R.id.seekBarValue);
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress = progress / 50;
            progress = progress * 50;
            seekBarValue.setText(String.valueOf(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });




}

}

new_plan.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="77dp" />

<TextView
    android:id="@+id/seekBarValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/seekbar"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="28dp"
    android:text="Button" />


LOG WINDOW:

02-24 21:52:50.601: E/AndroidRuntime(2236): FATAL EXCEPTION: main
02-24 21:52:50.601: E/AndroidRuntime(2236): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hakkikonu.dailycalorie/com.hakkikonu.dailycalorie.NewPlan}: java.lang.NullPointerException
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.os.Looper.loop(Looper.java:137)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at java.lang.reflect.Method.invoke(Method.java:511)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at dalvik.system.NativeStart.main(Native Method)
02-24 21:52:50.601: E/AndroidRuntime(2236): Caused by: java.lang.NullPointerException
02-24 21:52:50.601: E/AndroidRuntime(2236):     at com.hakkikonu.dailycalorie.NewPlan.onCreate(NewPlan.java:19)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.app.Activity.performCreate(Activity.java:5104)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-24 21:52:50.601: E/AndroidRuntime(2236):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-24 21:52:50.601: E/AndroidRuntime(2236):     ... 11 more

Solution

  • Change:

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

    to:

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

    Your ID in the XML file is seekBar. Java is case sensitive.