我的程序包含一个名为date的按钮,用于显示日历视图。但应用程序在单击时结束。请给我一个解决方案。我的程序包含一个名为date的按钮来显示日历视图。但应用程序在单击时结束。请给我一个解决方案。
FirstActivity.java
package example.showevent1;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.os.Bundle;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.*;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class FirstActivity extends FragmentActivity implements OnItemSelectedListener, OnDateSetListener {
/** Called when the activity is first created. */
private static final String DATE_FORMAT = "yyyy-MM-dd";
classdbOpenHelper eventsData;
TextView userSelection;
Button okButton;
Button date1;
public EditText date;
private Calendar mCalendar;
private static final String[] items = { "Yalahanka", "Rajai nagar", "Sivaji Nagar", "Koramangala", "RT Nagar", "Banashankari", "Yashwanthpura", "Hebbal" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
okButton = (Button) findViewById(R.id.button2);
date1 = (Button) findViewById(R.id.button1);
userSelection = (TextView) findViewById(R.id.textView1);
Spinner my_spin = (Spinner) findViewById(R.id.spinner1);// data1
my_spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
my_spin.setAdapter(aa);
okButton.setOnClickListener(new clicker());
eventsData = new classdbOpenHelper(this);
date1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePicker();
}
});
}
static final String YEAR = "year";
static final String MONTH = "month";
static final String DAY = "day";
static final String HOUR = "hour";
static final String MINS = "mins";
static final String CALENDAR = "calendar";
private void showDatePicker() {
Object ft = getFragmentManager().beginTransaction();
DatePickerDialogFragment newFragment = new DatePickerDialogFragment();
Bundle args = new Bundle();
args.putInt(YEAR, mCalendar.get(Calendar.YEAR));
args.putInt(MONTH, mCalendar.get(Calendar.MONTH));
args.putInt(DAY, mCalendar.get(Calendar.DAY_OF_MONTH));
newFragment.setArguments(args);
newFragment.show((FragmentManager) ft, "datePicker");
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) {
userSelection.setText(items[pos]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
userSelection.setText("");
}
class clicker implements Button.OnClickListener {
public void onClick(View v) {
String datevalue = date1.getText().toString();
String Userselectvalue = userSelection.getText().toString();
SQLiteDatabase db = eventsData.getWritableDatabase();
ContentValues cv = new ContentValues();
// cv.put(classdbOpenHelper.KEY_COUNTED,
// metersave.getText().toString());
cv.put(classdbOpenHelper.KEY_DESC, Userselectvalue);
cv.put(classdbOpenHelper.KEY_DATE, datevalue);
db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv);
}
public void onDestroy() {
eventsData.close();
}
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, monthOfYear);
mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateButtons();
}
private void updateButtons() {
// Set the date button text
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String dateForButton = dateFormat.format(mCalendar.getTime());
date1.setText(dateForButton);
}
}
DatePickerDialogFragment.java
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.*;
public class DatePickerDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
Fragment editFragment = getFragmentManager().findFragmentByTag(FirstActivity.ACCESSIBILITY_SERVICE);
OnDateSetListener listener = (OnDateSetListener) editFragment;
return new DatePickerDialog(getActivity(), listener,
args.getInt(FirstActivity.YEAR),
args.getInt(FirstActivity.MONTH),
args.getInt(FirstActivity.DAY));
}
}
答案 0 :(得分:1)
您正在获取Nullpointer,因为您从未实际初始化Calendar对象:
mCalendar