空指针异常 - 片段活动

Posted

技术标签:

【中文标题】空指针异常 - 片段活动【英文标题】:Null pointer exception - Fragment Activity 【发布时间】:2013-10-18 14:04:12 【问题描述】:

我对此很陌生,并且已经设法获得了我的应用程序的布局,并且正在使用选项卡和片段。

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

我要开始工作的第一件事是 BMI 片段的微调器设置。 我是在 MainActivity 还是 Fragment 中执行此操作?从我到目前为止所做的事情来看,我得到了一个空指针异常。

这是我的 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;
        
    


我的片段代码:

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)

【问题讨论】:

问题和标题不匹配.. 正确编辑问题 也发布你的 logcat.. 什么是BMI片段?你可以发布你的布局的截图。我不明白 getItem 方法的用途... 对不起。不好解释。 BMI 片段实际上是上面的代码 DummySectionFragment。我现在已经添加了 Logcat。感谢您的意见。 【参考方案1】:

从此改变 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;

onViewCreated 致电setupSpinners

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

如果您将view 作为参数传递会更有效,所以不要每次都调用getView()

【讨论】:

【参考方案2】:

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

onCreateView() 的主体中,您调用setupSpinners(); 来初始化您的成员,然后您重新初始化它们。仅在一处初始化它们。

【讨论】:

感谢您的反馈,我现在已经完成了,但仍然遇到相同的 logcat 错误。 我已经用我更新的片段代码更新了帖子。【参考方案3】:
@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);

【讨论】:

【参考方案4】:

您的所有视图都未在您的 DummySectionFragment 中初始化。 您必须说出每个 Spinner 是什么,例如:

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

【讨论】:

感谢您的反馈,我现在已经这样做了,但仍然遇到相同的 logcat 错误。 现在,您在 onCreate 视图方法中调用“getView()”。视图尚未创建!您必须在 onCreateView 方法中删除 setup 方法中的初始化并用您的“rootView”替换“getView()”。这是您正在寻找的视图的一个实例。

以上是关于空指针异常 - 片段活动的主要内容,如果未能解决你的问题,请参考以下文章

空指针异常 - 片段活动

访问片段的子视图时出现空指针异常

在使用ViewPager时尝试从其父活动修改片段时出现空指针异常

操作栏上不确定的进度条上的空指针异常

Android - 接口callBack抛出空指针异常

片段中的空指针异常