0

我设计并实现了一个增加 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) {

        }
    });




}

}

新计划.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" />


日志窗口:

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
4

2 回答 2

2

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

于 2013-02-24T22:12:25.800 回答
1

改变:

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

到:

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

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

于 2013-02-24T22:23:48.200 回答