weal Infotech weal Infotech - 2 months ago 8
Android Question

'android.view.View android.view.View.findViewById(int)' on a null object reference

I am trying to display Date on the

TextView
with the help of
datePicker
but it is generating the error like.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.sachin.datepicker.MainActivity.findViewsById(MainActivity.java:44)
at com.example.sachin.datepicker.MainActivity.onCreateView(MainActivity.java:37)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


And here, is my java file

MainActivity.java

package com.example.sachin.datepicker;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;
import java.util.Locale;

public class MainActivity extends Fragment implements View.OnClickListener {
Activity activity;

public MainActivity() {};
private EditText fromDateEtxt;
private EditText toDateEtxt;
private EditText toDateEtxt1;
private DatePickerDialog fromDatePickerDialog;
private DatePickerDialog toDatePickerDialog;
private DatePickerDialog toDatePickerDialog1;
private SimpleDateFormat dateFormatter;

View view;
@TargetApi(Build.VERSION_CODES.N)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.activity_add_wod, container, false);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

findViewsById();

setDateTimeField();
return view;
}

private void findViewsById() {
fromDateEtxt = (EditText) view.findViewById(R.id.openingdate);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();

toDateEtxt = (EditText)view. findViewById(R.id.Birthdate);
toDateEtxt.setInputType(InputType.TYPE_NULL);
toDateEtxt.requestFocus();
toDateEtxt1 = (EditText)view. findViewById(R.id.Anniversary);
toDateEtxt1.setInputType(InputType.TYPE_NULL);
}

private void setDateTimeField() {
fromDateEtxt.setOnClickListener(this);
toDateEtxt.setOnClickListener(this);
toDateEtxt1.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {

@TargetApi(Build.VERSION_CODES.N)
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}

},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

toDatePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {

@TargetApi(Build.VERSION_CODES.N)
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
toDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}

},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
toDatePickerDialog1 = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {

@TargetApi(Build.VERSION_CODES.N)
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
toDateEtxt1.setText(dateFormatter.format(newDate.getTime()));
}

},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}

// @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View view) {
if(view == fromDateEtxt) {
fromDatePickerDialog.show();
} else if(view == toDateEtxt) {
toDatePickerDialog.show();
} else if(view == toDateEtxt1) {
toDatePickerDialog1.show();
}
}
}


manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sachin.sachin">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="DateFragment"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>


I tried to fixed the error but it is not working so please suggest me for this error...

Answer

change

private void findViewsById()

to

private void findViewsById(View view)

and inside onCreateView

findViewsById(view);
Comments