Java Android SeekBar应用程序-不幸的是,项目已经停止
我是Android开发的新手,对于我的项目,我正在创建两个Seekbar。第一个seekbar的最小值为50,最大值为180,而第二个seekbar的最小值为180,最大值为400。我还在每个seekbar下面添加了textview标签,以显示每个seekbar的当前值。我的代码编译得很好,没有任何错误,并在Android emulator上显示得很好,但当我单击滑块更改seekbar的值时,我的应用程序最终崩溃并说:“很遗憾,您的项目已停止。”下面是我的代码: MainActivity.javaJava Android SeekBar应用程序-不幸的是,项目已经停止,java,android,xml,android-layout,android-activity,Java,Android,Xml,Android Layout,Android Activity,我是Android开发的新手,对于我的项目,我正在创建两个Seekbar。第一个seekbar的最小值为50,最大值为180,而第二个seekbar的最小值为180,最大值为400。我还在每个seekbar下面添加了textview标签,以显示每个seekbar的当前值。我的代码编译得很好,没有任何错误,并在Android emulator上显示得很好,但当我单击滑块更改seekbar的值时,我的应用程序最终崩溃并说:“很遗憾,您的项目已停止。”下面是我的代码: MainActivity.jav
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>
提前谢谢你 你的搜索栏的最小值是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);
}
崩溃可能是由于以下原因:
lowtext.setText(lowbarmin);
hightext.setText(highbarmin);
当您执行此操作时,lowbarmin和highbarmin的值都是整数,android将使用这些值作为其resourceId查找字符串资源,但找不到
因此,您要做的是将字符串值传递给setText():
你的日志在哪里?发布它。为什么你增加int-lowbarmin=progress+50;int highbarmin=进度+180???好了,我终于发现我的错误了。它来自于没有将lowbarmin和highbarmin转换为字符串。谢谢大家的帮助!
lowtext.setText(Integer.toString(lowbarmin));
hightext.setText(Integer.toString(highbarmin));