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) {

        }
    });




}

}

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" />


ログ ウィンドウ:

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 を持つべき View を見つけることができませんseekbar。で使用されている ID も再確認する必要がありnew_plan.xmlます。プロジェクトのクリーニングと再構築もお勧めします。私の経験から、一部の変更は強制的に生成されず、Java コードから のR.javaメンバーを参照するときに混乱する問題が発生します。R

于 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 に答える