RecyclerView Problems With The Adapter [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
Alright I'm trying to display data called by an api on a recyclerview and I get the error. It has nothing to do with butterknife I think. Please help.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)'
on a null object reference
at com.example.myflickrproject.PhotoDisplayView.onCreate(PhotoDisplayView.java:40)
public class PhotoDisplayView extends AppCompatActivity { //implements AdapterView.OnItemClickListener
#BindView(R.id.recView)
RecyclerView rViewDis;
#BindView(R.id.addToDatabse)
Button download;
#BindView(R.id.backButton)
Button goBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_display_view);
System.out.println("My photolist is " + DataCollection.photoList);
rViewDis.setLayoutManager(new GridLayoutManager(this, 3));
PhotoAdapter myAdapter = new PhotoAdapter(this, DataCollection.photoList);
rViewDis.setAdapter(myAdapter);
ButterKnife.bind(this);
}
#OnClick({R.id.addToDatabse, R.id.backButton})
public void onClick(View v) {
int btns = v.getId();
switch (btns) {
case R.id.addToDatabse:
addToDatabse();
break;
case R.id.backButton:
backToMenu();
break;
}
}
public void addToDatabse() {
}
public void backToMenu() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}

Bind your views after setContentView(R.layout.activity_photo_display_view);
So, your code should be like this
setContentView(R.layout.activity_photo_display_view);
ButterKnife.bind(this);

Related

error: method onCreate(Bundle) is already defined in class MainActivity public void onCreate(Bundle icicle) { ^ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I am new to programming and I was making an app in Android Studio. I wanted to make several buttons open a new Activity, but when repeating the process for the other buttons it gave me this error.
There's my code:
public class MainActivity extends AppCompatActivity {
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.octubre);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
private Button button2;
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, Activity3.class);
startActivity(startIntent);
button2 = (Button) findViewById(R.id.aucas);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openActivity3();
}
});
}
public void openActivity3() {
Intent intent = new Intent(this, Activity3.class);
startActivity(intent);
}
}
You may have two redundant onCreate methods. Activity class should have only one onCreate method. Please remove the method 'public void onCreate(Bundle icicle)' from your code. The onCreate method shown on the above code hasn't any issue. So, you can leave it as it is. I hope this will solve your issue.

Android app crashes when activity is initialised from fragment [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 9 months ago.
I am currently trying to create a calendar app in android studio. The app contains a calendar with different "Views" e.g. monthly view, weekly view, and daily view.
The app uses fragments as pages and each view is an activity.
This is the code for the fragment and buttons to initialise each view
public class CalendarFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_calendar, container, false);
Button btn1 = (Button) v.findViewById(R.id.openCalendar);
btn1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
Intent intent = new Intent(getActivity(),CalendarActivity.class);
startActivity(intent);
}
});
Button btn2 = (Button) v.findViewById(R.id.openWeek);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(getActivity(), WeekViewActivity.class);
startActivity(intent);
}
});
return v;
}
The first button which displays the monthly view of the calendar called "CalendarActivity" in code works fine but when the second button is clicked which displays the weekly view of the calendar, causes the app to crash and gives the following error in the logcat
2022-05-13 14:44:59.642 15183-15183/com.example.finalyearproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.finalyearproject, PID: 15183
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.finalyearproject/com.example.finalyearproject.WeekViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)' on a null object reference
at com.example.finalyearproject.CalendarUtils.monthYearFromDate(CalendarUtils.java:16)
at com.example.finalyearproject.WeekViewActivity.setWeekView(WeekViewActivity.java:40)
at com.example.finalyearproject.WeekViewActivity.onCreate(WeekViewActivity.java:29)
at android.app.Activity.performCreate(Activity.java:8054)
at android.app.Activity.performCreate(Activity.java:8034)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loopOnce(Looper.java:201) 
at android.os.Looper.loop(Looper.java:288) 
at android.app.ActivityThread.main(ActivityThread.java:7842) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
I am not sure what the problem is as I have been following a tutorial and the other features work. I have also included the code for each activity below.
Monthly View
public class CalendarActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener {
private TextView monthYearText;
private RecyclerView calendarRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
initWidgets();
CalendarUtils.selectedDate = LocalDate.now();
setMonthView();
}
private void initWidgets()
{
calendarRecyclerView = findViewById(R.id.calendarRecyclerView);
monthYearText = findViewById(R.id.monthYearTV);
}
private void setMonthView()
{
monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
ArrayList<LocalDate> daysInMonth = daysInMonthArray(CalendarUtils.selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
}
public void nextMonth(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusMonths(1);
setMonthView();
}
public void previousMonth(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusMonths(1);
setMonthView();
}
Weekly View
public class WeekViewActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener {
private TextView monthYearText;
private RecyclerView calendarRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_week_view);
initWidgets();
setWeekView();
}
private void initWidgets()
{
calendarRecyclerView = findViewById(R.id.calendarRecyclerView);
monthYearText = findViewById(R.id.monthYearTV);
}
private void setWeekView()
{
monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
ArrayList<LocalDate> days = daysInWeekArray(CalendarUtils.selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(days, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
}
public void previousWeek(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusWeeks(1);
setWeekView();
}
public void nextWeek(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusWeeks(1);
setWeekView();
}
Any suggestions would be greatly appreciated.
My suggestions:
Firstly you didn't initialized CalendarUtils.selectedDate
Cover code with breakpoints for. debugging or with Log.d() messages to find, which variable is null

App crashing because of NullPinterException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I'm trying to make an Intent to start the NewEventActivityunfortunately the app crashejn when the floating action button is pressed. Here is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newEventIntent(View v){
Intent intent = new Intent(this, NewEventActivity.class);
startActivity(intent);
}
}
activity_main.xml:
<android.support.design.widget.FloatingActionButton
android:onClick="newEventIntent"
android:id="#+id/add_event_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:src="#drawable/baseline_add_24"/>
LogCat:
java.lang.RuntimeException: Unable to start activity componentInfo{com.example.deadline/com.example.deadline.NewEventActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
I made sure that both activities are in the manifests.
The reason is that you are trying to set a listener to a FloatingActionButton that it does not have reference in your layout.
You should find it by id :
Add private FloatingActionButton mFloatingActionButton;
and
floatingActionButton=findViewById(R.id.add_event_fab);
public class MainActivity extends AppCompatActivity {
private FloatingActionButton mFloatingActionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatingActionButton=findViewById(R.id.add_event_fab);
}
public void newEventIntent(View v){
Intent intent = new Intent(this, NewEventActivity.class);
startActivity(intent);
}
}

Why does this code give me a null pointer exception? [duplicate]

This question already has answers here:
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
(9 answers)
When do activity's instance variables get initialized?
(2 answers)
Closed 4 years ago.
public class FindBeerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_beer);
}
Spinner color = (Spinner) findViewById(R.id.color);
TextView brands = (TextView) findViewById(R.id.brands);
public void onClickFindBeer(View view) {
//Get the selected item in the spinner
String beerType = String.valueOf(color.getSelectedItem());
//Display the selected item
brands.setText(beerType);
}
}
I know if I move the findviewbyid's into the onclick method, the code will run fine but i would like to understand why it wont work this way even though the setContentView has already been called.
Perhaps you misplaced the closing curly-brace. This one should fix your NPE.
public class FindBeerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_beer);
Spinner color = (Spinner) findViewById(R.id.color);
TextView brands = (TextView) findViewById(R.id.brands);
}
public void onClickFindBeer(View view) {
//Get the selected item in the spinner
String beerType = String.valueOf(color.getSelectedItem());
//Display the selected item
brands.setText(beerType);
}
}

NullPointerException while trying to set click listener on button which is on bottomsheet [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
i have recyclerview in my App and when list item is pressed bottomsheet dialog expends where i have two buttons but i can't set click listeners to it.
This is onClick method in adapter
BottomSheetDialog offer_info_dialog;
RelativeLayout rel;
Button Yes,No;
View parentView;
#Override
public void onClick(View view) {
rel = (RelativeLayout) activity.findViewById(R.id.bottomsheet);
Yes = (Button) offer_info_dialog.findViewById(R.id.confirm_btn_on_info);
No = (Button) offer_info_dialog.findViewById(R.id.cancel_btn_on_info);
offer_info_dialog = new BottomSheetDialog(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
parentView = inflater.inflate(R.layout.offer_info_layout, null);
offer_info_dialog.setContentView(parentView);
((View) parentView.getParent()).setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
offer_info_dialog.show();
rel.setVisibility(View.INVISIBLE);
Yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.removeCheatOfferMarkers(2);
}
});
No.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) parentView.getParent());
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
offer_info_dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
rel.setVisibility(View.VISIBLE);
}
});
}
and this is an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.BottomSheetDialog.findViewById(int)' on a null object reference
Multiple problems here.
You are calling findViewById before you have inflated your view. This will cause nullpointer exceptions. findViewById should only be called AFTER inflater.inflate has been called, except in the case of your activity which I assume has already been inflated.
Take a look at your code. offer_info_dialog has not been initialized to anything before you try to start calling methods on it, which is why you are getting a nullpointer.

Categories