简体   繁体   English

NPE onCreate导致Android片段崩溃

[英]Android Fragment Crashing from NPE onCreate

I'm stuck and a new pair of eyes would be appreciated. 我被困住了,不胜感激。 I have my MainActivity calling a fragment on a button click. 我让MainActivity在单击button调用一个fragment It was pulling it in fine, then I added database calls in the fragment and now its crashing. 一切正常,然后我在fragment添加了database调用,现在崩溃了。 In my logcat its saying im getting a null pointer exception in my onCreate. 在我的logcat中,它的说法是在我的onCreate中获取空指针异常。 Here is the code and logcat. 这是代码和logcat。

fragment code 片段代码

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;
    }
}

button handler 按钮处理程序

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)

Set this view = inflater.inflate(R.layout.stats_frag, container, false); 设置此view = inflater.inflate(R.layout.stats_frag, container, false); before getting your views as below 在获得如下观点之前

Use getActivity() instead of getView() while getting the views. 获取视图时,请使用getActivity()而不是getView()

ie

   TextView daysText = (TextView) getActivity().findViewById(R.id.days); 
    TextView totalText = (TextView)  getActivity().findViewById(R.id.total); 
    TextView aveText = (TextView) getActivity().findViewById(R.id.ave);

Move this closer to the top before you grab the views. 在获取视图之前,将其移到顶部附近。 You are getting an NPE because the findViewById calls are returning null. 您正在获得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);

before you grab the views. 在您获取视图之前。 Then do 然后做

TextView daysText = (TextView) view.findViewById(R.id.days); 
TextView totalText = (TextView) view.findViewById(R.id.total); 
TextView aveText = (TextView) view.findViewById(R.id.ave);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM