我被困住了,一双新眼睛会受到赞赏。我MainActivity
点击fragment
点击button
database
。它把它拉得很好,然后我在fragment
中添加了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;
}
}
次调用,现在它已经崩溃了。在我的logcat中,它说我在onCreate中得到一个空指针异常。这是代码和logcat。
片段代码
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();
}
按钮处理程序
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)
logcat的
{{1}}
答案 0 :(得分:2)
在获取您的观看之前设置此view = inflater.inflate(R.layout.stats_frag, container, false);
获取视图时使用getActivity()
代替getView()
。
即
TextView daysText = (TextView) getActivity().findViewById(R.id.days);
TextView totalText = (TextView) getActivity().findViewById(R.id.total);
TextView aveText = (TextView) getActivity().findViewById(R.id.ave);
答案 1 :(得分:1)
在抓取视图之前将其移近顶部。您正在获取NPE,因为findViewById调用返回null。
// 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);
在抓住视图之前。然后做
TextView daysText = (TextView) view.findViewById(R.id.days);
TextView totalText = (TextView) view.findViewById(R.id.total);
TextView aveText = (TextView) view.findViewById(R.id.ave);