SeekBar停止活动|错误是什么?

时间:2013-02-24 21:56:25

标签: android seekbar

我设计并实现了一个搜索栏,其中有50个被攻击。但是如果我运行应用程序就会崩溃。没有语法错误(Eclipse没有注意到这一点)有什么问题?

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

2 个答案:

答案 0 :(得分:2)

您的Java代码找不到应具有标识seekbar的视图。您还应该仔细检查new_plan.xml中使用的ID。我建议也清理你的项目和重建。根据我的经验,一些更改不会强制生成R.java,然后当您从Java代码引用R的成员时,您会看到令人困惑的问题。

答案 1 :(得分:1)

变化:

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

为:

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

您在XML文件中的ID为seekBar。 Java区分大小写。