Intent传递Date会导致null

时间:2017-10-17 20:49:15

标签: java android android-intent nullpointerexception

我搜索并发现没有解释为什么null是Intent的结果,例如:

How to get Date extra from an intent?

Android: Passing Date in putExra

似乎Intent没有在我的代码中传递任何内容,因为我的代码导致NullPointerException(我正在通过" Android编程"由Big Nerd Ranch完成):

首先,我声明并定义密钥EXTRA_DATE并在CrimeFragment的代码中创建Intent(注意:这是代码下面显示的LogCat输出中使用的TAG),在else语句中:

href="../../static/css/bootstrap.min.css"

从此代码部分(下面)的LogCat输出中可以看出,' date'变量在此时具有一个值,就在使用Intant发送之前:

      public static final String EXTRA_DATE = "date";
      private static Date date = null;
      ...

        if (small screen) {
             //small screen code
        } else {
            Log.d(TAG, "Large screen: activity");
            date = mCrime.getDate();
            Log.d(TAG, "date is " + date);
            Intent intent = new Intent(getContext(), DatePickerActivity.class);
            intent.putExtra(EXTRA_DATE, date);
            startActivityForResult(intent, REQUEST_DATE);
        }

startActivityForResult然后启动DatePickerActivity,并进入createFragment(Android Studio建议我添加导入,我做了):

CrimeFragment: Large screen: activity
CrimeFragment: date is Mon Oct 16 18:19:24 CDT 2017

如import语句中所述,在import语句中引用了DatePickerFragment:

import static com.bignerdranch.android.criminalintent.DatePickerFragment.EXTRA_DATE;

public class DatePickerActivity extends SingleFragmentActivity {
    private final String TAG = "DatePickerActivity";
    private static final int REQUEST_DATE = 0;

@Override
protected Fragment createFragment() {
    Log.d(TAG, "Entered createFragment");
    Date date = (Date) getIntent().getSerializableExtra(EXTRA_DATE);
    Log.d(TAG,"date = " + date);
    return DatePickerFragment.newInstance(date);
}

扩展了SingleFragmentActivity:

              public static DatePickerFragment newInstance(Date date){
                    Log.d(TAG, "Entered newInstance");
                    Log.d(TAG, "Date is " + date);
                    Bundle args = new Bundle();
                    args.putSerializable(EXTRA_DATE, date);
                    DatePickerFragment fragment = new DatePickerFragment();
                    fragment.setArguments(args);
                   return fragment;
               }

但是,如Logcat行显示的那样意图立即失败,其中' date = null':

        public abstract class SingleFragmentActivity extends AppCompatActivity {
            protected abstract Fragment createFragment();
            private static final String TAG= "SingleFragmentActivity";

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Log.d(TAG, "Entered onCreate");
                setContentView(R.layout.activity_fragment);

                FragmentManager fm = getSupportFragmentManager();
                 //fragment_container is the top level FrameLayout in activity_fragment
                Fragment fragment = fm.findFragmentById(R.id.fragment_container);

                if (fragment == null){
                    fragment = createFragment();
                    fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
                }
            }
        }

此null值现在传递给DatePickerFragment:

CrimeFragment: Entered onPause
CrimeFragment: Entered onPause
SingleFragmentActivity: Entered onCreate
DatePickerActivity: Entered createFragment
DatePickerActivity: date = null

直到NullPointerException发生:

public class DatePickerFragment extends DialogFragment {
    public static final String EXTRA_DATE = "com.bignerdranch.android.criminalintent.date";
    private static final String ARG_DATE = "Date";
    private static final String TAG = "DatePickerFragment";

    private DatePicker mDatePicker;

public static DatePickerFragment newInstance(Date date){
    Log.d(TAG, "Entered newInstance");
    Log.d(TAG, "Date is " + date);
    Bundle args = new Bundle();
    args.putSerializable(ARG_DATE, date);
    DatePickerFragment fragment = new DatePickerFragment();
    fragment.setArguments(args);
    return fragment;
}

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                       @Nullable Bundle savedInstanceState){
    Log.d(TAG, "Entered onCreateView");
    Date date = (Date) getArguments().getSerializable(EXTRA_DATE);
    Log.d(TAG, "Date equals: " + date);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

如上面在DatePickerActivity的LogCat输出中所见,日期变量为null,因此从该点开始传递null,如上面的onCreateView代码中的以下LogCat片段所示。

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.bignerdranch.android.criminalintent/com.bignerdranch. 
android.criminalintent.DatePickerActivity}: java.lang.NullPointerException:
Attempt to invoke virtual method ‘long java.util.Date.getTime()’ on a null 
object reference

通过我的所有阅读,包括stackoverflow.com中的所有帖子,似乎这应该正常工作,Intent应该简单地将Date值从CrimeFragment传递给DatePickerFragment,但它并不是&#t的值。 39;日期等于null'示出。

似乎对我来说没有什么可以做的,Intent应该只是通过' date',但它没有。

2 个答案:

答案 0 :(得分:0)

老大书呆子牧场男人带我回来了。

DatePickerActivity不知道extra_date是什么。 在你的课堂上没有它的参考,它不是一个变量。

通常意图的额外内容就像

intent.putExtra(String key,Object object);

你做了intent.putExtra(EXTRA_DATE,date);这很好 但在检索类中,您需要知道EXTRA_DATE值是什么。

答案 1 :(得分:0)

感谢sdfbhg,AChez9和Barns52!你们钉了它们。因为我没有真正理解关键问题,所以我没有正确识别EXTRA_DATE键。所以,现在指向那个方向,我搜索了一个新的焦点,发现:

naming convention for passing data through extras in android

指出:

https://developer.android.com/training/basics/firstapp/starting-activity.html#StartActivity

在该页面的代码中,有答案:

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

请注意,源文件的名称是密钥的一部分。所以,我在DatePickerActivity的代码中更改了一行:

    Date date = (Date) intent.getSerializableExtra(EXTRA_DATE);

要:

    Date date = (Date) intent.getSerializableExtra(CrimeFragment.EXTRA_DATE);

NullPointerException不再是因为我现在引用了检索文件中的源文件。课程是正确引用您的密钥。现在我知道我不知道。

还有一件事。我试验并发现添加: import static com.bignerdranch.android.criminalintent.CrimeFragment.EXTRA_DATE;提供了Date date = (Date) intent.getSerializableExtra(EXTRA_DATE);正常工作的信息。因此,有两种方法可以为您的Intent调用提供关键参考信息。