我被卡住了,一双新的眼睛将不胜感激。我的MainActivity拨打fragment,点击button。它正在拉动它,然后我在fragment中添加database呼叫,现在它正在崩溃。在我的logcat中,它说我的onCreate中得到一个空指针异常。这里是代码和logcat。从NPE崩溃的Android片段onCreate
片段代码
public class StatsFragment extends Fragment {
public static MySmokinDatabase mySmokinDatabase;
public static Model model;
View view;
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
model = MainActivity.model;
mySmokinDatabase = new MySmokinDatabase(getActivity());
TextView daysText = (TextView) getView().findViewById(R.id.days);
TextView totalText = (TextView) getView().findViewById(R.id.total);
TextView aveText = (TextView) getView().findViewById(R.id.ave);
try {
daysText.setText("" + (model.getDays()));
totalText.setText("" + mySmokinDatabase.getTotal());
if (model.getDays() > 0)
aveText.setText("" + (float) mySmokinDatabase.getTotal()/(float) model.getDays());
}
catch (Exception e) {
Log.v("*** DEBUG ***", "Error");
System.out.print("error" + e);
}
// Create, or inflate the Fragment’s UI, and return it.
// If this Fragment has no UI then return null.
view = inflater.inflate(R.layout.stats_frag, container, false);
return view;
}
}
按钮处理
public void statisticsHandler(View view) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Log.v("*** DEBUG ***", "Statistics Handler");
ft.replace(R.id.main_container_Id, new StatsFragment());
ft.commit();
}
logcat的
03-27 15:40:03.767: W/dalvikvm(862): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-27 15:40:03.867: E/AndroidRuntime(862): FATAL EXCEPTION: main
03-27 15:40:03.867: E/AndroidRuntime(862): java.lang.NullPointerException
03-27 15:40:03.867: E/AndroidRuntime(862): at com.example.smokin4tomsullivan.StatsFragment.onCreateView(StatsFragment.java:24)
03-27 15:40:03.867: E/AndroidRuntime(862): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
03-27 15:40:03.867: E/AndroidRuntime(862): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
03-27 15:40:03.867: E/AndroidRuntime(862): at android.app.BackStackRecord.run(BackStackRecord.java:635)
03-27 15:40:03.867: E/AndroidRuntime(862): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
03-27 15:40:03.867: E/AndroidRuntime(862): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
03-27 15:40:03.867: E/AndroidRuntime(862): at android.os.Handler.handleCallback(Handler.java:615)
03-27 15:40:03.867: E/AndroidRuntime(862): at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 15:40:03.867: E/AndroidRuntime(862): at android.os.Looper.loop(Looper.java:137)
03-27 15:40:03.867: E/AndroidRuntime(862): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-27 15:40:03.867: E/AndroidRuntime(862): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 15:40:03.867: E/AndroidRuntime(862): at java.lang.reflect.Method.invoke(Method.java:511)
03-27 15:40:03.867: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-27 15:40:03.867: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-27 15:40:03.867: E/AndroidRuntime(862): at dalvik.system.NativeStart.main(Native Method)