こんにちは、私は Android 開発のまったくの初心者で、この問題について何週間も頭を悩ませてきました。基本的に私は SeekBar を持っており、SeekBar が移動するたびに何かを実行したいと考えています。onSeekBarChange イベント ハンドラーをセットアップしようとしましたが、追加するとすぐにアプリが実行されなくなり、起動時にクラッシュします。私はオンラインであらゆる場所を見てきましたが、見つけたすべての例は私のコードと正確に一致しているので、間違った場所に置いているのではないかと思っています。これが MainActivity クラス ファイル全体です。
package com.slistersoft.scottlisterlab11;
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
String custName, phoneNum, carChoice;
double miles, costBeforeTip, tipPercentage, tipAmount, totalCost;
private SeekBar tipBar = null;
public void MsgBox(String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void showToast(String message, int durationMS){
Toast.makeText(getApplicationContext(), message, durationMS).show();
}
public void calculateCost(View v){
final EditText nameBox = (EditText)findViewById(R.id.nameBox);
final EditText phoneBox = (EditText)findViewById(R.id.phoneNumber);
final EditText milesBox = (EditText)findViewById(R.id.milesBox);
final Spinner car = (Spinner)findViewById(R.id.carPickerDropDown);
final SeekBar tipBar = (SeekBar)findViewById(R.id.tipBar);
if(nameBox.getText().toString().equals(""))
showToast("Please enter your name", 5000);
else if(phoneBox.getText().toString().equals("") )
showToast("Please enter your phone number",5000);
else if(milesBox.getText().toString().equals(""))
showToast("Please enter the amount of miles you need to travel",5000);
else{
custName = nameBox.getText().toString();
phoneNum = phoneBox.getText().toString();
miles = Double.parseDouble(milesBox.getText().toString());
carChoice = car.getSelectedItem().toString();
costBeforeTip = (3.25 * miles) + 3.00;
tipPercentage = tipBar.getProgress();
tipAmount = (tipPercentage/100) * costBeforeTip;
totalCost = costBeforeTip + tipAmount;
DecimalFormat currency = new DecimalFormat("$###,###.##");
MsgBox("Hello " + custName + ",\n\n" +
"Your estimated cost will be " + currency.format(costBeforeTip) +
"\n\nWith a " + tipPercentage + "% tip of " + currency.format(tipAmount) + " that brings your total cost to " + currency.format(totalCost) +
"\n\nA " + carChoice + " will pick you up ASAP. \n\nWe will call you at " + phoneNum + " when we are getting close. \n\nThank you for using CrAzY TaXi.");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tipBar = (SeekBar)findViewById(R.id.tipBar);
tipBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
誰かがこれについて何かアイデアを持っているなら、私はそれを大いに感謝します.