Frage

Hi I am a total newb to Android Development and I have been banging my head against the wall about this for weeks. Basically I have a SeekBar and I want to have it do something whenever the SeekBar moves. I tried to set up an onSeekBarChange event handler and as soon as I added that the app will not run anymore, and crashes at launch. I have looked all over the place online, and all the examples I find match my code EXACTLY, so I'm just wondering if I am putting it in the wrong place or something. Here is my entire MainActivity class file.

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;
    }
}

}

If anybody has any ideas on this I would greatly appreciate it.

War es hilfreich?

Lösung

does the seekbar belong to fragment_main.xml? I am guess it does. So move initialization to onCreateView of fragment. use rootView.findViewById – Raghunandan 10 hours ago

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top