嗨,我正在获得 NPE
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3100)
at android.view.View.performClick(View.java:3627)
at android.view.View$PerformClick.run(View.java:14304)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4512)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3095)
... 11 more
Caused by: java.lang.NullPointerException
at com.rkapps.calendarsharing.SyncToCloudActivity.getDateFromDatePicket(SyncToCloudActivity.java:59)
at com.rkapps.calendarsharing.SyncToCloudActivity.syncWithCloud(SyncToCloudActivity.java:47)
从下面的代码
package com.rkapps.calendarsharing;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import com.rkapps.calendarsharing.events.Synchronizer;
public class SyncToCloudActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sync_to_cloud);
DatePicker startDatePicker = (DatePicker) findViewById(R.id.datePicker1);
DatePicker endDatePicker = (DatePicker) findViewById(R.id.datePicker2);
setNormnalCalendar(startDatePicker);
setNormnalCalendar(endDatePicker);
}
private void setNormnalCalendar(DatePicker startDatePicker) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 11) {
try {
Method m = startDatePicker.getClass().getMethod("setCalendarViewShown", boolean.class);
m.invoke(startDatePicker, false);
}
catch (Exception e) {} // eat exception in our case
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_sync_to_cloud, menu);
return true;
}
public void syncWithCloud(View view) {
DatePicker startDatePicker = (DatePicker) view.findViewById(R.id.datePicker1);
Date startDate = getDateFromDatePicket(startDatePicker);
DatePicker endDatePicker = (DatePicker) view.findViewById(R.id.datePicker2);
Date endDate = getDateFromDatePicket(endDatePicker);
new Synchronizer(getContentResolver(), this).syncToCloud(startDate ,endDate);
}
/**
*
* @param datePicker
* @return a java.util.Date
*/
public static java.util.Date getDateFromDatePicket(DatePicker datePicker) {
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
return calendar.getTime();
}
}
NPE 来自我试图从 syncWithCloud() 访问我的日期选择器的行——当按下同一活动中的按钮时调用该方法。
我从 android frums 中读到我应该使用 DialogFragmnet 来使用这个 DatePicker。但由于我希望我的应用程序支持 android 2.3,并且不想参与 supoprt 库的版本控制复杂性,所以我选择不使用 Dialog 片段,无论如何我可以使用 Datepickers 吗?提前致谢 。
活动xml如图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_date" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/end_date" />
<DatePicker
android:id="@+id/datePicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sync_to_cloud"
android:onClick="syncWithCloud"/>
</LinearLayout>