当我在触发onSelectDate事件时尝试调用AsyncTask时,我得到了这个运行时错误(lang.java.nullPointerException)。我花了几个小时来通过谷歌搜索解决这个错误,但我得到一个线索,有一个对象丢失。这是我的代码。而且我也希望有人让我摆脱这个错误。 感谢。
public class CaldroidSampleActivity extends FragmentActivity{
@Override
protected View onCreateView(LayoutInflater inflater) {
setRetainInstance(true);
View v = inflater.inflate(R.layout.activity_main,null);
final SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
// Setup caldroid fragment
final CaldroidFragment caldroidFragment = new CaldroidFragment();
// Setup arguments
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt("month", cal.get(Calendar.MONTH) + 1);
args.putInt("year", cal.get(Calendar.YEAR));
caldroidFragment.setArguments(args);
FragmentTransaction t = this.getFragmentManager().beginTransaction();
t.replace(R.id.calendar1, caldroidFragment);
t.commit();
// Setup listener
final CaldroidListener listener = new CaldroidListener() {
@Override
public void onSelectDate(Date date, View view) {
**callGetSelectedDateDetail(this,date);**
Toast.makeText(context, formatter.format(date),
Toast.LENGTH_LONG).show();
}
@Override
public void onChangeMonth(int month, int year) {
String text = "month: " + month + " year: " + year;
Toast.makeText(context, text, Toast.LENGTH_LONG)
.show();
}
};
caldroidFragment.setCaldroidListener(listener);
return v;
}
**protected void callGetSelectedDateDetail(CaldroidListener caldroidListener,
Date d)** {
executeGetSelectedDateDetail(context, d);
// TODO Auto-generated method stub
}
public void executeGetSelectedDateDetail(Context c, Date d)
{
if(c!=null){
new getSelectedDateDetail(c, d).execute();
}
}
}
public class getSelectedDateDetail extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
if(context!=null){
Log.i("onPreExecute","onPreExecute");
}
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
Log.i("doInBackground","ok");
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(context!=null){
Log.i("onPostExecute","onPostExecute");
}
}
}
这是我在运行时得到的错误......
04-07 04:53:24.790: E/AndroidRuntime(2247): FATAL EXCEPTION: main
04-07 04:53:24.790: E/AndroidRuntime(2247): java.lang.NullPointerException
04-07 04:53:24.790: E/AndroidRuntime(2247): at android.view.LayoutInflater.from(LayoutInflater.java:210)
04-07 04:53:24.790: E/AndroidRuntime(2247): at com.bc.ema.base.BaseAdapter.<init>(BaseAdapter.java:72)
04-07 04:53:24.790: E/AndroidRuntime(2247): at com.bc.ema.async.getSelectedDateDetail.<init>(getSelectedDateDetail.java:32)
04-07 04:53:24.790: E/AndroidRuntime(2247): at com.bc.ema.view.Fragment_MySchedule.executeGetSelectedDateDetail(Fragment_MySchedule.java:124)
04-07 04:53:24.790: E/AndroidRuntime(2247): at com.bc.ema.view.Fragment_MySchedule.callGetSelectedDateDetail(Fragment_MySchedule.java:117)
04-07 04:53:24.790: E/AndroidRuntime(2247): at com.bc.ema.view.Fragment_MySchedule$1.onSelectDate(Fragment_MySchedule.java:94)
04-07 04:53:24.790: E/AndroidRuntime(2247): at com.caldroid.CaldroidFragment$1.onItemClick(CaldroidFragment.java:410)
04-07 04:53:24.790: E/AndroidRuntime(2247): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-07 04:53:24.790: E/AndroidRuntime(2247): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
04-07 04:53:24.790: E/AndroidRuntime(2247): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
04-07 04:53:24.790: E/AndroidRuntime(2247): at android.widget.AbsListView$1.run(AbsListView.java:3423)
04-07 04:53:24.790: E/AndroidRuntime(2247): at android.os.Handler.handleCallback(Handler.java:725)
04-07 04:53:24.790: E/AndroidRuntime(2247): at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 04:53:24.790: E/AndroidRuntime(2247): at android.os.Looper.loop(Looper.java:137)
04-07 04:53:24.790: E/AndroidRuntime(2247): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-07 04:53:24.790: E/AndroidRuntime(2247): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 04:53:24.790: E/AndroidRuntime(2247): at java.lang.reflect.Method.invoke(Method.java:511)
04-07 04:53:24.790: E/AndroidRuntime(2247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-07 04:53:24.790: E/AndroidRuntime(2247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-07 04:53:24.790: E/AndroidRuntime(2247): at dalvik.system.NativeStart.main(Native Method)