Android SeekBar应用程序 - 不幸的是,项目已停止

时间:2015-03-18 05:54:37

标签: java android xml android-layout android-activity

我是Android开发人员的新手,对于我的项目,我正在创建两个搜索栏。第一个搜索栏的最小值为50,最大值为180,而第二个搜索栏的最小值为180,最大值为400.我还在每个搜索栏下方添加了文本视图标签,以显示每个搜索栏的当前值。我的代码编译得很好,没有任何错误,并在Android模拟器上显示正常,但当我点击滑块更改搜索栏的值时,我的应用程序最终崩溃并说,"不幸的是,您的项目已经停止。 #34;以下是我的代码:

MainActivity.java

    package com.example.myname.projectname;

    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.SeekBar.OnSeekBarChangeListener;
    import android.R.*;
    public class MainActivity extends ActionBarActivity implements      OnSeekBarChangeListener {

        private SeekBar lowBar;
        private SeekBar highBar;
        private TextView lowtext;
        private TextView hightext;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           initializevariables();
           lowBar = (SeekBar)findViewById(R.id.lowbar);
           highBar = (SeekBar)findViewById(R.id.highbar);
           lowtext = (TextView)findViewById(R.id.lowval);
           hightext = (TextView)findViewById(R.id.highval);

           lowBar.setOnSeekBarChangeListener(this);
           highBar.setOnSeekBarChangeListener(this);

       }


       @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);
       }
       private void initializevariables(){
           lowBar = (SeekBar) findViewById(R.id.lowbar);
           highBar = (SeekBar) findViewById(R.id.highbar);
       }

       public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
       {
           progress = 0;
           int lowbarmin = progress + 50;
           int highbarmin = progress + 180;
           lowtext.setText(lowbarmin);
           hightext.setText(highbarmin);
       }

       @Override
       public void onStartTrackingTouch(SeekBar seekBar) {

       }

       @Override
       public void onStopTrackingTouch(SeekBar seekBar) {

       }
}

activity_main.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"  android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"  tools:context=".MainActivity">

<EditText android:text="CGM Alarm System" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<SeekBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lowbar"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:indeterminate="false"
    android:progress="0"
    android:max="180"/>

<SeekBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/highbar"
    android:layout_below="@+id/lowbar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="66dp"
    android:layout_alignRight="@+id/lowbar"
    android:layout_alignEnd="@+id/lowbar"
    android:progress="0"
    android:max="400"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="50"
    android:id="@+id/lowval"
    android:layout_below="@+id/lowbar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="180"
    android:id="@+id/highval"
    android:layout_below="@+id/highbar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

</RelativeLayout>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

</resources>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myname.projectname"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
         android:name="com.example.myname.projectname.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

提前谢谢!

2 个答案:

答案 0 :(得分:0)

您的搜索栏的最小值为50,您不能增加+50;

   public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
   {
       int lowbarmin = progress + 1;
       int highbarmin = progress + 1;
       lowtext.setText(lowbarmin);
       hightext.setText(highbarmin);
   }           

答案 1 :(得分:0)

崩溃可能是因为以下几行:

lowtext.setText(lowbarmin);
hightext.setText(highbarmin);

当你这样做并且lowbarmin和highbarmin的值是整数时,android会查找字符串资源,并将这些值作为其resourceId,但找不到它。

所以你想要做的是,将字符串值传递给setText():

lowtext.setText(Integer.toString(lowbarmin));
hightext.setText(Integer.toString(highbarmin));