CalendarView 导致 App 崩溃

CalendarView Causes App to Crash

基本上我的 calendarView 小部件有问题。在应用程序中,无论何时从我的 Main class 转到 EditProgress class,它都会冻结或崩溃。我测试了它,每当 calendarView 小部件位于 EditProgress class 中时,应用程序运行非常缓慢,每当我尝试在我的 .java class 中使用该小部件时,它就会崩溃。它在说它需要 API 级别 11 之前曾抱怨过,所以我试图通过使用抑制警告来解决这个问题,但它仍然崩溃或运行缓慢。感谢您的任何帮助。下面是我的 .java class:

package com.dwolford.workoutroutine;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


public class EditProgress extends Activity {

Button submit;
Button back;
Spinner exercise;
ArrayAdapter<CharSequence> adapter;
EditText reps;
CalendarView calendar;
Context context = this;

@TargetApi(11)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_progress);


    exercise = (Spinner)findViewById(R.id.spinner);
    adapter = ArrayAdapter.createFromResource(this, R.array.Exercises, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    exercise.setAdapter(adapter);
    exercise.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText((getBaseContext()), (parent.getItemIdAtPosition(position) + 2) + " selected", Toast.LENGTH_LONG).show();
        }

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

        }
    });


    calendar = (CalendarView)findViewById(R.id.calendarView);
    reps = (EditText)findViewById(R.id.editText);
    submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            double repetitions = Double.parseDouble(reps.getText().toString());//Get number of repetitions on that day

            String exerciseName = exercise.getSelectedItem().toString();//Get specific exercise user did

            String calendarDate = calendar.getDate()+"";
        }
    });



    back = (Button)findViewById(R.id.Back);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           finish();
        }
    });
}

@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_edit_progress, 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);
}
}

这是我的 .XML 文件:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="EDIT PROGRESS"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="45dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:height="50dp"
    android:textSize="22dp"
    android:gravity="center"
    android:textStyle="bold" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BACK"
    android:id="@+id/Back"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:height="50dp"
    android:textSize="22dp"
    android:background="@drawable/button_gradient" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:id="@+id/Submit"
    android:layout_above="@+id/Back"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="38dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:height="50dp"
    android:textSize="22dp"
    android:background="@drawable/button_gradient" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Exercise"
    android:id="@+id/Exercise"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="20dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Date"
    android:id="@+id/date"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:textSize="20dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Repetitions"
    android:id="@+id/repetitions"
    android:layout_alignBottom="@+id/date"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textSize="20dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@+id/repetitions"
    android:layout_alignRight="@+id/repetitions"
    android:layout_alignEnd="@+id/repetitions"
    android:layout_alignLeft="@+id/repetitions"
    android:layout_alignStart="@+id/repetitions" />

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_below="@+id/Exercise"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toStartOf="@+id/date"
    android:layout_toLeftOf="@+id/date"
    android:layout_alignRight="@+id/Exercise"
    android:layout_alignEnd="@+id/Exercise" />

<CalendarView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/calendarView"
    android:layout_below="@+id/date"
    android:layout_centerHorizontal="true" />

您用于该项目的最低 SDK 低于 API11。这就是它给您该错误的原因。

您可以在项目设置中更改它。只需将其增加到 API 11,一切都会正常进行。