创建片段时出现空指针异常

时间:2013-12-26 08:29:17

标签: android android-fragments

我正在尝试实施Sliding Menu。当用户点击按钮时,我想显示两个片段,一个用于单击照片,另一个用于选择日期和时间。我正在使用DatePickerFragment来选择日期。我收到了NullPointerException

MainActivity()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displayView(0);
    }
}
private void displayView(int position) {
        // update the main content by replacing fragments
        //HomeFragment homeFragment = null;
        //ImageFragment imageFragment=null;
        Fragment fragment=null;
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        DateTimeFragment datetime=null;
        switch (position) {
        case 0:
            fragment= new HomeFragment();
            break;

        case 1: 
            fragment=new ImageFragment();
            datetime=new DateTimeFragment();
            break;

        default:
            break;
        }

        if(fragment!=null)
        {
            ft.replace(R.id.frame_container, fragment);
            if(datetime!=null)
            {
                ft.replace(R.id.datetime_container, datetime);
            }
            ft.commit();
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

DateTimeFragment

import java.util.Calendar;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageButton;

public class DateTimeFragment extends Fragment {

     private EditText editStartDate;
     private EditText editEndDate;
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
       { 
         View v = inflater.inflate(R.layout.fragment_image, container, false);
         final Calendar calendar = Calendar.getInstance();

            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH)+1;
            int dd = calendar.get(Calendar.DAY_OF_MONTH);

            ImageButton startDate=(ImageButton)v.findViewById(R.id.start_date_button);
            editStartDate = (EditText)v.findViewById(R.id.startDate);
            editStartDate.setText(Integer.toString(dd)+ "/" + Integer.toString(mm) +"/"+ Integer.toString(yy));

            ImageButton endDate=(ImageButton)v.findViewById(R.id.end_date_button);
            editEndDate = (EditText)v.findViewById(R.id.endDate);
            editEndDate.setText(Integer.toString(dd)+ "/" + Integer.toString(mm) +"/"+ Integer.toString(yy));

             AutoCompleteTextView location= (AutoCompleteTextView)v.findViewById(R.id.location);
             location.setAdapter(new PlacesAutoCompleteAdapter(getActivity(),R.layout.list_layout));

             startDate.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                           try
                            {
                               DialogFragment newFragment= (DialogFragment) new DatePickerFragment(R.id.startDate);
                               newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
                            }

                            catch(Exception e)
                            {

                                return ;
                            }
                    }
                });

             endDate.setOnClickListener(new View.OnClickListener() {
             @Override
                public void onClick(View v) {
                            try
                            {
                                 DialogFragment newFragment= (DialogFragment) new DatePickerFragment(R.id.endDate);
                                 newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");

                            }
                            catch(Exception e)
                            {
                                return ;
                            }
            }
        });

         return v;
      }

     public void onComplete(String date, int id) {
            if(id==R.id.start_date_button)
             editStartDate.setText(date);
            else if(id==R.id.end_date_button)
             editEndDate.setText(date); 

        }
}

imageFragment本身显示正确,但当我尝试显示DateFragment时,出现错误,如下所示:

12-26 16:17:39.023: E/AndroidRuntime(1799): FATAL EXCEPTION: main
12-26 16:17:39.023: E/AndroidRuntime(1799): Process: com.example.makemyday, PID: 1799
12-26 16:17:39.023: E/AndroidRuntime(1799): java.lang.NullPointerException
12-26 16:17:39.023: E/AndroidRuntime(1799):     at com.example.makemyday.DateTimeFragment.onCreateView(DateTimeFragment.java:31)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.os.Handler.handleCallback(Handler.java:733)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.os.Looper.loop(Looper.java:136)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at java.lang.reflect.Method.invoke(Method.java:515)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-26 16:17:39.023: E/AndroidRuntime(1799):     at dalvik.system.NativeStart.main(Native Method)

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

正如您所看到的,您在第31行的DateTimeFragment类中的代码抛出了Null Pointer Exception。可能有些像ImageButtons这样的布局元素不是布局的一部分(或使用不同的Id),因此您的{{1 }}返回findViewById()