Android Studio SeekBar错误

时间:2015-04-27 18:07:30

标签: java android

您好我想在android studio中获取SeekBar的值。如果我运行app它会崩溃。没有语法错误。

package prosis.guiaturistico;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity  {

private static SeekBar seek_bar;
private static TextView text_view;

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

public void seekbar(){
    seek_bar = (SeekBar)findViewById(R.id.seekbar);
    text_view = (TextView)findViewById(R.id.seekbarValue);
    text_view.setText("Covered : " + seek_bar.getProgress() + " / " +seek_bar.getMax());

    seek_bar.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener(){
                int progress_value;
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    progress_value = progress;
                    text_view.setText("Covered : " + progress + " / " +seek_bar.getMax());
                    Toast.makeText(MainActivity.this,"SeekBar in progress",Toast.LENGTH_LONG).show();
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                    Toast.makeText(MainActivity.this,"SeekBar in start",Toast.LENGTH_LONG).show();
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    text_view.setText("Covered : " + progress_value + " / " +seek_bar.getMax());

                    Toast.makeText(MainActivity.this,"SeekBar in stop",Toast.LENGTH_LONG).show();
                }
            }
    );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

这是我的布局。

<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekbar"
android:layout_above="@+id/bt_pes"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/bt_mon"
android:max="5000" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/seekbarValue"
android:layout_above="@+id/seekbar"
android:layout_below="@+id/bt_caf"
android:layout_alignEnd="@+id/bt_pes"    
android:layout_alignStart="@+id/bt_pes" />

当我运行时,我收到这些错误消息,模拟器说不幸的是你的程序已停止。

 04-27 16:26:18.615    2846-2846/prosis.guiaturistico E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: prosis.guiaturistico, PID: 2846
java.lang.RuntimeException: Unable to start activity ComponentInfo{prosis.guiaturistico/prosis.guiaturistico.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.SeekBar.getProgress()' on a null object reference
        at        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at          
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at a   android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.SeekBar.getProgress()' on a null object reference
        at prosis.guiaturistico.MainActivity.seekbar(MainActivity.java:27)
        at prosis.guiaturistico.MainActivity.onCreate(MainActivity.java:21)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at          android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)

1 个答案:

答案 0 :(得分:-1)

问题在于这一行:

text_view.setText("Covered : " + seek_bar.getProgress() + " / " +seek_bar.getMax());

seek_bar.getProgress()seek_bar.getMax()都返回原始int值。 TextView.setText()方法倾向于假设原始int值为资源ID。

要避免这种情况,请使用String.valueof()将原始值转换为String表示形式。或者更好的是,使用StringBuilder而不是普通的字符串连接。