微调器的实现导致我的应用程序崩溃

时间:2016-04-18 11:00:59

标签: android

这是Java端。它具有Spinner适配器以及setOnItemClickListener方法。当我评论" spTitles" setadAdapter方法,程序运行。但是无论我是否评论spCategories setAdapter方法

,程序都会运行
import java.util.ArrayList;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

import com.playmaker.BudgetOh.R;
import com.playmaker.BudgetOh.adapter.CategoriesAdapter;
import com.playmaker.BudgetOh.manager.CategoriesManager;
import com.playmaker.BudgetOh.manager.ExpenseManager;
import com.playmaker.BudgetOh.model.Expense;

public class EditExpense extends Activity implements OnClickListener,
        OnItemSelectedListener, OnDateSetListener, OnTimeSetListener {
    private Spinner spTitles, spCategories;
    ;
    private String cat_item;
    private ArrayList<Expense> allExpenses;
    private ArrayList<String> allTitles;
    private Expense expense;
    private String titlePosition;
    private ExpenseManager expenseManager;
    private Button btnEditExpenseDate, btnEditExpenseTime, btnAddExpense; //actually modify expense;
    private EditText etEditExpenseTitle, etEditExpenseAmount, etEditExpenseComments;
    private int mYear, mDay, mMonth, mHour, mMinute;
    static final int DATE_DIALOG_ID = 0;
    static final int TIME_DIALOG_ID = 1;
    private ArrayAdapter<String> catAdapter;
    private String title, date, time, amount, category, comment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_expense);
        init();

        allExpenses = expenseManager.getAllExpenses();

        for (Expense expense : allExpenses) {
            allTitles.add(expense.getTitle());
        }


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, allTitles);

        //Populting the categories
        String[] osArray = {"Food and Drinks", "Transportation", "Health", "Leisure", "Other"};
        catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, osArray);

        spTitles.setAdapter(adapter);
        spCategories.setAdapter(catAdapter);


    }

    private void init() {
        expenseManager = new ExpenseManager(this);
        expense = new Expense();

        //For Spinners
        spTitles = (Spinner) findViewById(R.id.spTitles);
        spCategories = (Spinner) findViewById(R.id.spCategories);

        //For Buttons
        btnEditExpenseTime = (Button) findViewById(R.id.btnEditExpenseTime);
        btnEditExpenseDate = (Button) findViewById(R.id.btnEditExpenseDate);
        btnAddExpense = (Button) findViewById(R.id.btnModifyExpense);

        //For EditText
        etEditExpenseTitle = (EditText) findViewById(R.id.etEditExpenseTitle);
        etEditExpenseAmount = (EditText) findViewById(R.id.etEditExpenseAmount);
        etEditExpenseComments = (EditText) findViewById(R.id.etEditExpenseComments);

        //Setting Onclick listeners for all the buttons

        btnEditExpenseDate.setOnClickListener(this);
        btnEditExpenseTime.setOnClickListener(this);
        btnAddExpense.setOnClickListener(this);
        spTitles.setOnItemSelectedListener(this);
        spCategories.setOnItemSelectedListener(this);

        allExpenses = new ArrayList<Expense>();
        allTitles = new ArrayList<String>();


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnEditExpenseTime:
                showDialog(TIME_DIALOG_ID);
                break;
            case R.id.btnEditExpenseDate:
                showDialog(DATE_DIALOG_ID);

                break;
            case R.id.btnModifyExpense:
                time = btnEditExpenseTime.getText().toString();
                date = btnEditExpenseDate.getText().toString();
                title = etEditExpenseTitle.getText().toString();
                category = cat_item;
                amount = etEditExpenseAmount.getText().toString();
                comment = etEditExpenseComments.getText().toString();

                expense.setTitle(title);
                expense.setDate(date);
                expense.setTime(time);
                expense.setAmount(amount);
                expense.setCategory(category);
                expense.setComment(comment);

                long ret = expenseManager.updateExpense(expense);
                if (ret != -1) {
                    clearFields();
                    Toast.makeText(getApplicationContext(), "Entry Added"
                            , Toast
                            .LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Failed-Entry"
                            , Toast
                            .LENGTH_LONG).show();
                }
                expenseManager.close();
                break;
        }
    }


    private void clearFields() {
        etEditExpenseComments.setText("");
        etEditExpenseAmount.setText("");
        etEditExpenseTitle.setText("");
        btnEditExpenseDate.setText("Date");
        btnEditExpenseTime.setText("Time");
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        mHour = hourOfDay;
        mMinute = minute;
        updateTime();
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDate();
    }

    private void updateDate() {
        this.btnEditExpenseDate.setText(new StringBuilder().append(mMonth + 1).append("-")
                .append(mDay).append("-").append(mYear).append(" "));
    }

    private void updateTime() {
        this.btnEditExpenseTime.setText(new StringBuilder().append(mHour).append(":")
                .append(mMinute).append(" "));
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
                               long arg3) {

        switch (arg0.getId()) {

            case R.id.spTitles:
                titlePosition = allTitles.get(position);
                expense = expenseManager.getExpense(titlePosition);

                etEditExpenseComments.setText(expense.getComment());
                etEditExpenseAmount.setText(expense.getAmount());
                etEditExpenseTitle.setText(expense.getTitle());
                break;

            case R.id.spCategories:
                cat_item = arg0.getItemAtPosition(position).toString();
                break;


            default:
                break;
        }
    }


    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:

                return new DatePickerDialog(this, this, mYear, mMonth, mDay);
            case TIME_DIALOG_ID:
                return new TimePickerDialog(this, this, mHour, mMinute, true);
            default:
                break;
        }
        return null;
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

        switch (arg0.getId()) {
            case R.id.spTitles:
                etEditExpenseTitle.setText("");
                etEditExpenseAmount.setText("");
                etEditExpenseComments.setText("");
                break;
            default:
                break;
        }


    }


}

这是xml文件

   <LinearLayout 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:orientation="vertical"
tools:context="com.playmaker.BudgetOh.activities.EditExpense">

<Spinner
android:id="@+id/spTitles"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />


<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<EditText
android:id="@+id/etEditExpenseTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:gravity="center"
android:hint="Expense Title"
android:paddingTop="10dp"></EditText>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/btnEditExpenseDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.86"
android:text="Date" />

<Button
android:id="@+id/btnEditExpenseTime"
android:layout_width="163dp"
android:layout_height="wrap_content"
android:text="Time" />
</LinearLayout>

<EditText
android:id="@+id/etEditExpenseAmount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:gravity="center"
android:hint="Expense Amount"
android:paddingTop="10dp"></EditText>


<TextView
android:id="@+id/tvCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:text="@string/CategoriesString"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="25sp"

/>

<Spinner
android:id="@+id/spCategories"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#E3CF57">

</Spinner>


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Comments"
android:textSize="25sp" />

<EditText
android:id="@+id/etEditExpenseComments"
android:layout_width="match_parent"
android:layout_height="70dp"
android:ems="10"
android:hint="Please enter you brief comments on your expense"
android:inputType="textMultiLine">

</EditText>

<Button
android:id="@+id/btnModifyExpense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Add Expense"
android:textSize="20sp" />


<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:orientation="horizontal">

<Button
android:id="@+id/action_bar_button_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="CANCEL" />

<Button
android:id="@+id/action_bar_button_ok"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="OK" />
</LinearLayout>


</LinearLayout>

<requestFocus />
</ScrollView>


</LinearLayout>

0 个答案:

没有答案