我正在尝试实现带有单选按钮和editText的警报对话框。当我单击editText时,我想观看另一个警报对话框,在其中可以设置日期等。 这是我的代码:
MovieActivity.java
package com.example.msnma.movienotifier;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.mikepenz.iconics.IconicsDrawable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MovieActivity extends BaseActivity {
private MovieFragment movieFrag;
private Context context;
//parte per il popup data
private EditText fromDateEtxt;
//private DatePickerDialog fromDatePickerDialog;
//private DatePickerDialog toDatePickerDialog;
//private SimpleDateFormat dateFormatter;
@BindView(R.id.toolbar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle("");
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.movie, menu);
menu.findItem(R.id.share)
.setIcon(new IconicsDrawable(this)
.icon(GoogleMaterial.Icon.gmd_share)
.color(Color.WHITE)
.sizeDp(24));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
case R.id.share:
if (movieFrag != null) {
movieFrag.shareMovie();
}
break;
}
return true;
}
@Override
protected void init() {
movieFrag = new MovieFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.container, movieFrag)
.commit();
}
//nuovo codice per alert dialog
public void alertFormElements()
{
/*
* Inflate the XML view. activity_main is in
* res/layout/form_elements.xml
*/
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.form_elements,
null, false);
// You have to list down your form elements
/*final CheckBox myCheckBox = (CheckBox) formElementsView
.findViewById(R.id.myCheckBox);*/
final RadioGroup genderRadioGroup = (RadioGroup) formElementsView
.findViewById(R.id.NotifyAlertRadioGroup);
//questo sarà sostituito con un calendario.
/*final EditText nameEditText = (EditText) formElementsView
.findViewById(R.id.nameEditText);*/
//parte data
fromDateEtxt = (EditText) formElementsView.findViewById(R.id.nameEditText);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();
//metodo data
//setDateTimeField();
//fromDatePickerDialog.show();
fromDateEtxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
DatePickerDialog dpd = new DatePickerDialog( context ,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
fromDateEtxt.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
},
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
dpd.show();
}
});
// the alert dialog
new AlertDialog.Builder(MovieActivity.this).setView(formElementsView)
.setTitle("Form Elements")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@TargetApi(11)
public void onClick(DialogInterface dialog, int id) {
//fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
String toastString = "";
/*
* Detecting whether the checkbox is checked or not.
*/
/*if (myCheckBox.isChecked()) {
toastString += "Happy is checked!\n";
} else {
toastString += "Happy IS NOT checked.\n";
}*/
/*
* Getting the value of selected RadioButton.
*/
// get selected radio button from radioGroup
int selectedId = genderRadioGroup
.getCheckedRadioButtonId();
// find the radiobutton by returned id
RadioButton selectedRadioButton = (RadioButton) formElementsView
.findViewById(selectedId);
/*toastString += "Selected radio button is: "
+ selectedRadioButton.getText() + "!\n";*/
/*
* Getting the value of an EditText.
*/
/*toastString += "Name is: " + nameEditText.getText()
+ "!\n";*/
//showToast(toastString);
dialog.cancel();
}
}).show();
}
public void pressEditNotifyButton(View view)
{
alertFormElements();
}
/*@Override
public void onClick(View view) {
if(view == fromDateEtxt)
fromDatePickerDialog.show();
}*/
//Intent myIntent = new Intent(view.getContext(), agones.class);
//startActivityForResult(myIntent, 0);
}
form_elements.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!--<CheckBox
android:id="@+id/myCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Happy?" />-->
<RadioGroup
android:id="@+id/NotifyAlertRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--android:layout_below="@+id/myCheckBox" >-->
<RadioButton
android:id="@+id/OneDayRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="1 day before" />
<RadioButton
android:id="@+id/ReleaseDayRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/OneDayRadioButton"
android:text="Release day" />
<RadioButton
android:id="@+id/OnRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ReleaseDayRadioButton"
android:text="On :" />
<EditText
android:id="@+id/nameEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:clickable="true"
android:inputType="date"
android:layout_alignParentLeft="true"
android:layout_toRightOf="@+id/OnRadioButton"
android:ems="10"
android:hint="Type your name here..." >
<requestFocus />
</EditText>
</RadioGroup>
</RelativeLayout>
问题是,当应用程序运行时,我到达第一个弹出窗口/警报,并且当我单击editText时,应用程序因以下错误而崩溃:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at android.app.DatePickerDialog.resolveDialogTheme(DatePickerDialog.java:142)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:115)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:90)
at com.example.msnma.movienotifier.MovieActivity$1.onClick(MovieActivity.java:145)
at android.view.View.performClick(View.java:5646)
at android.view.View$PerformClick.run(View.java:22459)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
MovieActivity.java:145行是这样的:
...
c.get(Calendar.DAY_OF_MONTH));
...
但是问题不外乎。 我该怎么解决?