空指针异常 - 片段活动

时间:2013-10-10 17:12:05

标签: android layout android-activity fragment

我对此很新,并设法获得我的应用程序的布局,并使用标签和片段。

每个布局都是一个不同的计算器,需要运行不同的计算。我正在努力弄清楚如何做到这一点。 非常感谢任何帮助。

我试图开始工作的第一件事是BMI片段的spinnersetup。 我是在MainActivity还是片段中执行此操作?从我到目前为止所做的,我得到一个空指针异常。

这是我的MainActivity代码:

public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            switch (position) {
            case 0:
                Fragment fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
                return fragment;

              case 1:
                Fragment fragment1 = new DummySectionFragment1();
                Bundle args1 = new Bundle();
                args1.putInt(DummySectionFragment1.ARG_SECTION_NUMBER, position + 2);
                fragment1.setArguments(args1);
                return fragment1;

              case 2:

                 Fragment fragment2 = new DummySectionFragment2();
                 Bundle args2 = new Bundle();
                 args2.putInt(DummySectionFragment2.ARG_SECTION_NUMBER, position + 3);
                 fragment2.setArguments(args2);
                 return fragment2;

              case 3:

                     Fragment fragment3 = new DummySectionFragment3();
                     Bundle args3 = new Bundle();
                     args3.putInt(DummySectionFragment3.ARG_SECTION_NUMBER, position + 4);
                     fragment3.setArguments(args3);
                     return fragment3;

            default:
                return null;
            }
        }

        @Override
        public int getCount() {
            // Show 4 total pages.
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            case 3:
                return getString(R.string.title_section4).toUpperCase(l);
            }
            return null;
        }
    }

}

My Fragment代码:

package com.HealthKicks.bodystats.admob;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class DummySectionFragment extends Fragment {

    EditText height_edit_text1, weight_edit_text1, height, weight;
    Spinner height_spinner, weight_spinner;
    Button calculatebmi;

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.bmi, container, false);

        setupSpinners();

        height_spinner =(Spinner) getView().findViewById(R.id.height_spinner);
        weight_spinner =(Spinner) getView().findViewById(R.id.weight_spinner);
        height_edit_text1 =(EditText) getView().findViewById(R.id.height_edit_text1);
        weight_edit_text1 =(EditText) getView().findViewById(R.id.weight_edit_text1);
        weight_spinner =(Spinner) getView().findViewById(R.id.weight_spinner);

        return rootView;
    }


    void setupSpinners() {

        height_spinner =(Spinner) getView().findViewById(R.id.height_spinner);
        weight_spinner =(Spinner) getView().findViewById(R.id.weight_spinner);
        height_edit_text1 =(EditText) getView().findViewById(R.id.height_edit_text1);
        weight_edit_text1 =(EditText) getView().findViewById(R.id.weight_edit_text1);

        // TODO Auto-generated method stub
            height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0){
                    height_edit_text1.setVisibility(View.GONE);
                } else {
                    height_edit_text1.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
                weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0){
                            weight_edit_text1.setVisibility(View.GONE);
                        } 
                        else if (position == 1){
                            weight_edit_text1.setVisibility(View.GONE);
                        }
                        else {
                            weight_edit_text1.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
    }   
}

logcat的:

10-10 18:38:40.959: E/AndroidRuntime(1118): FATAL EXCEPTION: main
10-10 18:38:40.959: E/AndroidRuntime(1118): java.lang.NullPointerException
10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.HealthKicks.bodystats.admob.DummySectionFragment.setupSpinners(DummySectionFragment.java:33)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.HealthKicks.bodystats.admob.DummySectionFragment.onCreateView(DummySectionFragment.java:26)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.View.measure(View.java:15848)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.View.measure(View.java:15848)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.View.measure(View.java:15848)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.View.measure(View.java:15848)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.Choreographer.doFrame(Choreographer.java:532)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.os.Handler.handleCallback(Handler.java:730)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.os.Looper.loop(Looper.java:137)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at java.lang.reflect.Method.invoke(Method.java:525)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 18:38:40.959: E/AndroidRuntime(1118):     at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:2)

从此处更改onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.bmi, container, false);
    setupSpinners();
    height_spinner = (Spinner) getView().findViewById(R.id.height_spinner);
    weight_spinner = (Spinner) getView().findViewById(R.id.weight_spinner);
    height_edit_text1 = (EditText) getView().findViewById(R.id.height_edit_text1);
    weight_edit_text1 = (EditText) getView().findViewById(R.id.weight_edit_text1);
    weight_spinner = (Spinner) getView().findViewById(R.id.weight_spinner);
    return rootView;
}

到此:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.bmi, container, false);
    height_spinner = (Spinner) rootView.findViewById(R.id.height_spinner);
    weight_spinner = (Spinner) rootView.findViewById(R.id.weight_spinner);
    height_edit_text1 = (EditText) rootView.findViewById(R.id.height_edit_text1);
    weight_edit_text1 = (EditText) rootView.findViewById(R.id.weight_edit_text1);
    weight_spinner = (Spinner) rootView.findViewById(R.id.weight_spinner);
    return rootView;
}

setupSpinners致电onViewCreated

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setupSpinners();
}

如果您将view作为参数传递,那么每次getView()都不会调用它会更有效:

答案 1 :(得分:0)

您正在对所有成员进行两次初始化。

onCreateView()的正文中,您调用setupSpinners();初始化您的成员,然后重新初始化它们。仅在一个地方初始化它们。

答案 2 :(得分:0)

您的DummySectionFragment中没有初始化任何视图。 你必须告诉每个Spinner是什么,例如:

height_spinner = (Spinner) getView().findViewById(R.id.theIdInTheLayoutFile);

答案 3 :(得分:0)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.bmi, container, false);
    height_spinner = (Spinner) rootView.findViewById(R.id.height_spinner);
    weight_spinner = (Spinner) rootView.findViewById(R.id.weight_spinner);
    height_edit_text1 = (EditText) rootView.findViewById(R.id.height_edit_text1);
    weight_edit_text1 = (EditText) rootView.findViewById(R.id.weight_edit_text1);
    weight_spinner = (Spinner) rootView.findViewById(R.id.weight_spinner);

    setUpSpinners(rootView);  
    return rootView;
}

protected void setUpSpinners(pass rootview here...)
{
      ...use spinners as 
      Spinner weight_spinner = (Spinner) rootView.findViewById(R.id.weight_spinner);
}