Android Fragment .setAdapter给出空指针Exception onClick

时间:2014-12-30 10:00:32

标签: android nullpointerexception fragment spinner

我有以下Android类:

package com.gloxci.studentreport;

import java.util.ArrayList;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;

public class ExamScheduleFragment extends CustomFragment{

   public ExamScheduleFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ViewPagerAdapter pAdapter=new ViewPagerAdapter(getActivity(), new RelativeLayout[]{setLayout1(inflater)});
        vp=new CustomViewPager(getActivity());
        vp.setAdapter(pAdapter);
        vp.setPagingEnabled(false);
        vp.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub

                    vp.setCurrentItem(1);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        return vp;
    }

       RelativeLayout setLayout1(LayoutInflater inflater)
    {
        final RelativeLayout rlayout2=(RelativeLayout)inflater.inflate(R.layout.attendance_stdlist, null);

        Button btnSave=(Button)rlayout2.findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stu

                ArrayList<String> subjects=new ArrayList<String>();
                subjects.add("Hello");
                subjects.add("Option1");
                subjects.add("Option2");

                Spinner ExamSpinner = (Spinner)rlayout2.findViewById(R.id.SpinnerExam);

                ArrayAdapter <String> subjectAdapter = new ArrayAdapter(getActivity(),
                        android.R.layout.simple_spinner_item, subjects);

                subjectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                ExamSpinner.setAdapter(subjectAdapter);


            }
        });
        return rlayout2;
    }


}

以及以下XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >



    <Button 
        android:id="@+id/btnSave"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Save"
        />

        <Spinner 
        android:id="@+id/SpinnerExam"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Exam Title"
        />



</RelativeLayout>

CustomFragment:

package com.gloxci.studentreport;

import android.app.Fragment;

public class CustomFragment extends Fragment{

     CustomViewPager vp;
     int SelectedIndex=-1;
     String lastTitle="";
     int onBack()
     {
         int retVal=vp.getCurrentItem();
         vp.previous();
         if(lastTitle.length()>0)
         MainActivity.setTitle(lastTitle);
         return retVal;
     }
}

问题是,当点击ExamSpinner.setAdapter(subjectAdapter);时,行nullpointerException会导致btnSave并导致应用崩溃。

据我所知,所有内容都存在,并且所有内容都已正确设置。完全相同的设置在另一个类中工作正常。

这是我正在制作的其他人创建的项目。我是新手。

以下是LogCat:

12-29 23:21:22.078: E/AndroidRuntime(4165): FATAL EXCEPTION: main
12-29 23:21:22.078: E/AndroidRuntime(4165): java.lang.NullPointerException
12-29 23:21:22.078: E/AndroidRuntime(4165):     at com.gloxci.studentreport.ExamScheduleFragment$2.onClick(ExamScheduleFragment.java:85)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at android.view.View.performClick(View.java:4084)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at android.view.View$PerformClick.run(View.java:16966)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at android.os.Handler.handleCallback(Handler.java:615)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at android.os.Looper.loop(Looper.java:137)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at java.lang.reflect.Method.invoke(Method.java:511)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 23:21:22.078: E/AndroidRuntime(4165):     at dalvik.system.NativeStart.main(Native Method)
12-29 23:24:09.020: E/Trace(10410): error opening trace file: No such file or directory (2)

2 个答案:

答案 0 :(得分:0)

来自xml布局的inflated layout is defferent,您的creating ViewPager instead of inflating the the xml layout

这样写:在onCreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.your_layout_name, null);

        return inflate;
    }

答案 1 :(得分:0)

对于任何有兴趣知道的人来说,问题在于我的设置,我需要使用listview来设置adapter。有效的代码如下:

final RelativeLayout rlayout2=(RelativeLayout)inflater.inflate(R.layout.attendance_stdlist, null);
ListView listview=(ListView)rlayout2.findViewById(R.id.list1);

然后设置我使用的适配器:

        Spinner ExamSpinner = (Spinner)rlayout2.findViewById(R.id.SpinnerExam);
        ListView listview=(ListView)rlayout2.findViewById(R.id.list1);

        ArrayAdapter <String> subjectAdapter = new ArrayAdapter(getActivity(),
                android.R.layout.simple_spinner_item, subjects);

        subjectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        listview.setAdapter(subjectAdapter);