当我尝试插入日期和时间选择器时,为什么我的应用程序不断崩溃? [英] Why does my app keep crashing when i try to insert a date and time picker?
本文介绍了当我尝试插入日期和时间选择器时,为什么我的应用程序不断崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class NewEvent extends Activity {
private static int RESULT_LOAD_IMAGE = 1 ;
私人EventHandler处理程序;
private String picturePath =;
private String name;
private String place;
private String date;
private String time;
私人字符串照片;
DatePicker datepicker; //对象datepicker
int年,月,日; //声明年,月和日的变量
TimePicker timepicker; //对象为timepicker
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.new_event);
handler = new EventHandler(getApplicationContext());
// datepicker =(DatePicker)findViewById(R.id.datePicker); //从layout
// timepicker =(TimePicker)findViewById(R.id.timePicker)获取datepicker属性; //从布局
获取timepicker属性timepicker.setIs24HourView(true); //将timepicker设置为24小时时钟视图
按钮按钮=(Button)findViewById(R.id.button1); //按钮显示日期戳值
按钮button1 =(Button)findViewById(R.id.button2); //按钮显示timepicker值
ImageView iv_user_photo =(ImageView)findViewById(R.id.iv_user_photo);
iv_user_photo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0){
Intent intent = new Intent(Intent .ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,RESULT_LOAD_IMAGE);
}
});
按钮btn_add =(Button)findViewById(R.id.btn_add);
btn_add.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0){
EditText et_name =(EditText)findViewById (R.id.et_name);
name = et_name.getText()。toString();
EditText et_place =(EditText)findViewById(R.id.et_place);
place = et_place.getText()。toString();
EditText et_date =(EditText)findViewById(R.id.et_date);
date = et_date.getText()。toString );
EditText et_time =(EditText)findViewById(R.id.et_time);
time = et_time.getText()。toString();
ImageView iv_photograph =(ImageView)findViewById(R.id.iv_user_photo);
photograph = picturePath;
事件事件=新事件();
event.setName(name);
event.setPlace(place);
event.setDate(date);
event.setTime(time);
event.setPhotograph(照片);
Boolean added = handler.addEventDetails(event);
if(added){
Intent intent = new Intent(NewEvent.this,MainEvent.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),Event data not added。Please try again,Toast.LENGTH_LONG).show();
}
}
});
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// TODO自动生成的方法存根
//显示datepicker值弹出通知
Toast.makeText(getBaseContext(),Date:
+(datepicker.getMonth()+ 1)+/+ datepicker.getDayOfMonth ()
+/+ datepicker.getYear(),Toast.LENGTH_LONG).show();}
});
//点击按钮开始timepicker事件
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
/ / TODO自动生成的方法stub
//显示timepicker值弹出通知
Toast.makeText(getBaseContext(),Time Selected:+ timepicker.getCurrentHour()+:
+ timepicker.getCurrentMinute(),Toast.LENGTH_SHORT).show();
}
});
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == RESULT_LOAD_IMAGE&&& resultCode == RESULT_OK&& null!= data){
Uri imageUri = data.getData();
String [] filePathColumn = {MediaStore.Images.Media.DATA};
游标cursor = getContentResolver()。query(imageUri,
filePathColumn,null,null,null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn [0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView =(ImageView)findViewById(R.id.iv_user_photo);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
这是我的xml:
<?xml version =1.0encoding =utf-8?>
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =match_parent
android:layout_height =match_parent
android:orientation =vertical
android:padding =10dp>
< TextView
android:id =@ + id / tv_new_event_title
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =添加新事件
android:textAppearance =?android:attr / textAppearanceLarge
android:layout_alignParentTop =true/>
< Button
android:id =@ + id / btn_add
android:layout_width =match_parent
android:layout_height = wrap_content
android:text =添加事件
android:layout_alignParentBottom =true/>
< ScrollView
android:layout_width =match_parent
android:layout_height =match_parent
android:layout_below =@ id / tv_new_event_title
android:layout_above =@ id / btn_add>
< LinearLayout
android:layout_width =fill_parent
android:layout_height =wrap_content
android:orientation =vertical>
< ImageView
android:id =@ + id / iv_user_photo
android:src =@ drawable / add_user_icon
android:layout_width =100dp
android:layout_height =100dp/>
< TextView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Event:/>
< EditText
android:id =@ + id / et_name
android:layout_width =match_parent
android:layout_height =wrap_content
android:ems =10
android:inputType =text>
< requestFocus />
< / EditText>
< TextView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Place:/ >
< EditText
android:id =@ + id / et_place
android:layout_width =match_parent
android:layout_height =wrap_content
android:ems =10
android:inputType =text>
< requestFocus />
< / EditText>
< TextView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Date:/>
< EditText
android:id =@ + id / et_date
android:layout_width =match_parent
android:layout_height =wrap_content
android:ems =10
android:inputType =date/>
< Button
android:id =@ + id / button
style =?android:attr / buttonStyleSmall
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Button/>
< requestFocus />
< TextView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Time:/>
< EditText
android:id =@ + id / et_time
android:layout_width =match_parent
android:layout_height =wrap_content
android:ems =10
android:inputType =time/>
< Button
android:id =@ + id / button1
style =?android:attr / buttonStyleSmall
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Button1/>
< requestFocus />
< / LinearLayout>
< / ScrollView>
< / RelativeLayout>
这是我的LogCat:
08-25 11:57:44.144:E / AndroidRuntime(554):FATAL EXCEPTION:main
/ pre>
08-25 11:57:44.144:E / AndroidRuntime(554):java。 lang.RuntimeException:无法启动活动ComponentInfo {com.chris.hockeymanagemtapp / com.chris.hockeymanagemtapp.NewEvent}:java.lang.NullPointerException
08-25 11:57:44.144:E / AndroidRuntime(554):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-25 11:57:44.144:E / AndroidRuntime(554):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-25 11:57:44.144:E / AndroidRuntime(554):android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
08-25 11:57 :44.144:E / AndroidRuntime(554):android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
08-25 11:57:44.144:E / AndroidRuntime(554) :在android.os.Handler.dispatchMessage(Handler.java:99)
08-25 11:57:44.144:E / Andr oidRuntime(554):在android.os.Looper.loop(Looper.java:123)
08-25 11:57:44.144:E / AndroidRuntime(554):在android.app.ActivityThread .main(ActivityThread.java:3683)
08-25 11:57:44.144:E / AndroidRuntime(554):java.lang.reflect.Method.invokeNative(Native Method)
08-25 11:57:44.144:E / AndroidRuntime(554):在java.lang.reflect.Method.invoke(Method.java:507)
08-25 11 :57:44.144:E / AndroidRuntime(554):com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 11:57:44.144: E / AndroidRuntime(554):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-25 11:57:44.144:E / AndroidRuntime(554):在dalvik.system.NativeStart.main(本地方法)
08-25 11:57:44.144:E / AndroidRuntime(554):引起:java.lang.NullPointerException
08-25 11:57:44.144:E / AndroidRuntime(554):在com.chris.hockeymanagemtapp.NewEvent.onCreate(NewEvent.java:60)
08-25 11:57: 44。 144:E / AndroidRuntime(554):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-25 11:57:44.144:E / AndroidRuntime(554):在android .app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-25 11:57:44.144:E / AndroidRuntime(554):... 11更多
解决方案在您的OnCreate方法中执行此操作:
DatePicker dt =(DatePicker)findViewById(R.id.yourDatePickerId);
正如Anuj所说,您需要在使用之前对其进行初始化
I am a novice with android and coding in general. I am trying to insert an android time and date picker into my code however it keeps crashing when I attempt to do so. Can anyone please help me out
public class NewEvent extends Activity { private static int RESULT_LOAD_IMAGE = 1; private EventHandler handler; private String picturePath = ""; private String name; private String place; private String date; private String time; private String photograph; DatePicker datepicker; // object for datepicker int year , month , day; // declaring variables for year, month and day TimePicker timepicker; // object for timepicker @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_event); handler = new EventHandler(getApplicationContext()); //datepicker = (DatePicker) findViewById(R.id.datePicker); // Obtain datepicker attributes from layout //timepicker = (TimePicker) findViewById(R.id.timePicker); //Obtain timepicker attributes from layout timepicker.setIs24HourView(true); //setting timepicker to 24 hr clock view Button button = (Button) findViewById(R.id.button1); // button to display datepicker value Button button1 = (Button) findViewById(R.id.button2); // button to display timepicker value ImageView iv_user_photo = (ImageView) findViewById(R.id.iv_user_photo); iv_user_photo.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, RESULT_LOAD_IMAGE); } }); Button btn_add = (Button) findViewById(R.id.btn_add); btn_add.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { EditText et_name = (EditText) findViewById(R.id.et_name); name = et_name.getText().toString(); EditText et_place = (EditText) findViewById(R.id.et_place); place = et_place.getText().toString(); EditText et_date = (EditText) findViewById(R.id.et_date); date = et_date.getText().toString(); EditText et_time = (EditText) findViewById(R.id.et_time); time = et_time.getText().toString(); ImageView iv_photograph = (ImageView) findViewById(R.id.iv_user_photo); photograph = picturePath; Event event = new Event(); event.setName(name); event.setPlace(place); event.setDate(date); event.setTime(time); event.setPhotograph(photograph); Boolean added = handler.addEventDetails(event); if(added){ Intent intent = new Intent(NewEvent.this, MainEvent.class); startActivity(intent); }else{ Toast.makeText(getApplicationContext(), "Event data not added. Please try again", Toast.LENGTH_LONG).show(); } } }); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // displaying datepicker value as pop up notification Toast.makeText(getBaseContext(), "Date : " + (datepicker.getMonth() + 1) +"/"+datepicker.getDayOfMonth() +"/"+datepicker.getYear(), Toast.LENGTH_LONG).show(); } }); // On clicking button start timepicker event button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // displaying timepicker value as pop up notification Toast.makeText(getBaseContext(),"Time Selected : "+timepicker.getCurrentHour()+":" +timepicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri imageUri = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); picturePath = cursor.getString(columnIndex); cursor.close(); ImageView imageView = (ImageView) findViewById(R.id.iv_user_photo); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); } } }
Here is my xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/tv_new_event_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add New Event" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" /> <Button android:id="@+id/btn_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add Event" android:layout_alignParentBottom="true" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/tv_new_event_title" android:layout_above="@id/btn_add"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/iv_user_photo" android:src="@drawable/add_user_icon" android:layout_width="100dp" android:layout_height="100dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Event:" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="text" > <requestFocus /> </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Place:" /> <EditText android:id="@+id/et_place" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="text" > <requestFocus /> </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date:" /> <EditText android:id="@+id/et_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="date" /> <Button android:id="@+id/button" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <requestFocus /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Time:" /> <EditText android:id="@+id/et_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="time" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <requestFocus /> </LinearLayout> </ScrollView> </RelativeLayout>
Here is my LogCat:
08-25 11:57:44.144: E/AndroidRuntime(554): FATAL EXCEPTION: main 08-25 11:57:44.144: E/AndroidRuntime(554): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chris.hockeymanagemtapp/com.chris.hockeymanagemtapp.NewEvent}: java.lang.NullPointerException 08-25 11:57:44.144: E/AndroidRuntime(554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 08-25 11:57:44.144: E/AndroidRuntime(554): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 08-25 11:57:44.144: E/AndroidRuntime(554): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 08-25 11:57:44.144: E/AndroidRuntime(554): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 08-25 11:57:44.144: E/AndroidRuntime(554): at android.os.Handler.dispatchMessage(Handler.java:99) 08-25 11:57:44.144: E/AndroidRuntime(554): at android.os.Looper.loop(Looper.java:123) 08-25 11:57:44.144: E/AndroidRuntime(554): at android.app.ActivityThread.main(ActivityThread.java:3683) 08-25 11:57:44.144: E/AndroidRuntime(554): at java.lang.reflect.Method.invokeNative(Native Method) 08-25 11:57:44.144: E/AndroidRuntime(554): at java.lang.reflect.Method.invoke(Method.java:507) 08-25 11:57:44.144: E/AndroidRuntime(554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-25 11:57:44.144: E/AndroidRuntime(554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-25 11:57:44.144: E/AndroidRuntime(554): at dalvik.system.NativeStart.main(Native Method) 08-25 11:57:44.144: E/AndroidRuntime(554): Caused by: java.lang.NullPointerException 08-25 11:57:44.144: E/AndroidRuntime(554): at com.chris.hockeymanagemtapp.NewEvent.onCreate(NewEvent.java:60) 08-25 11:57:44.144: E/AndroidRuntime(554): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-25 11:57:44.144: E/AndroidRuntime(554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 08-25 11:57:44.144: E/AndroidRuntime(554): ... 11 more
解决方案Inside your OnCreate method do this:
DatePicker dt = (DatePicker) findViewById(R.id.yourDatePickerId);
As Anuj said, you need to initialize it before using it
这篇关于当我尝试插入日期和时间选择器时,为什么我的应用程序不断崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文