Я создаю приложение для планирования, и когда я выбираю дату и нажимаю «Выбрать» для перехода к другому макету для создания задачи на эту дату, приложение останавливается.
Это мой основной код , который отлично работает:
package com.example.calendarview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;
public class CalendarActivity extends AppCompatActivity {
CalendarView calendarView;
TextView dateSelected;
Button selectButton;
Intent scheduleActivityIntent;
String date;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
selectButton = (Button) findViewById(R.id.selectButton);
selectButton.setOnClickListener(new CalendarView.OnClickListener(){
public void onClick(View v) {
System.out.println("Button Clicked");
scheduleActivityIntent = new Intent(getApplicationContext(), ScheduleActivity.class);
startActivity(scheduleActivityIntent);
}
});
calendarView = (CalendarView) findViewById(R.id.calendarView);
dateSelected = (TextView) findViewById(R.id.dateSelected);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@androidx.annotation.NonNull CalendarView view, int year,
int month, int dayOfMonth) {
date = dayOfMonth + "/" + (month+1) + "/" + year;
dateSelected.setText(date);
}
});
}
}
Это мое второе занятие, где я использую ListView для отображения текущих задач, что, я уверен, хорошо -
package com.example.calendarview;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import java.util.ArrayList;
public class ScheduleActivity extends CalendarActivity {
private static final String TAG = "ScheduleActivity";
ListView taskList;
Task haircut;
Task work;
ArrayList<Task> taskListAdapter;
ScheduleAdapter adapter;
String aTaskName = "Haircut";
String aTaskDate = "21/01/2020";
String aTaskTime = "14:00";
int aTaskTimeFinish = 1;
int aTaskUrgency = 2;
String aTaskDescription = "Go get a haircut";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
Log.d(TAG, "onCreate: Started");
taskList = (ListView) findViewById(R.id.schedule);
// Need to create object insert
haircut = new Task(aTaskName, aTaskDate, aTaskTime, aTaskTimeFinish, aTaskUrgency, aTaskDescription);
//Should be made as a function in order to insert user's tasks
//Creating Array adapter
taskListAdapter = new ArrayList<>();
taskListAdapter.add(haircut);
adapter = new ScheduleAdapter(getApplicationContext(), R.layout.activity_schedule, taskListAdapter);
taskList.setAdapter(adapter);
}
}
И это мой адаптер для ListView, где я почти уверен, что это проблематично c, но я действительно не понимаю проблему и как ее решить -
package com.example.calendarview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import java.util.ArrayList;
public class ScheduleAdapter extends ArrayAdapter <Task>
{
private static final String TAG = "ScheduleAdapter";
Context mContext;
Task task;
View cView;
TextView tvName;
TextView tvTime;
TextView tvUrgency;
LayoutInflater inflater;
//Default constructor for the adapter
public ScheduleAdapter(@NonNull Context context, int mResource, @NonNull ArrayList<Task> tasks) {
super(context,0, tasks);
}
@android.support.annotation.NonNull
@Override
public View getView(int position, @Nullable View convertView, @android.support.annotation.NonNull ViewGroup parent) {
cView = convertView;
inflater = LayoutInflater.from(mContext);
task = getItem(position);
if(cView == null) {
cView = LayoutInflater.from(getContext()).inflate(R.layout.schedule_items, parent, false);
}
tvName = cView.findViewById(R.id.task_name);
tvTime = cView.findViewById(R.id.task_time);
tvUrgency = cView.findViewById(R.id.task_urgency);
tvName.setText(task.getTaskName());
tvTime.setText(task.getTaskTime());
tvUrgency.setText(task.getTaskUrgency());
return cView;
}
}
Задача basi c object;
private String taskName;
private String taskDate;
private String taskTime;
private int taskTimeFinish;
private int taskUrgency;
private String taskDescription;
И я получаю эту ошибку-
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calendarview, PID: 7247
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:229)
at com.example.calendarview.ScheduleAdapter.getView(ScheduleAdapter.java:35)
at android.widget.AbsListView.obtainView(AbsListView.java:2372)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2171)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:758)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Process 7247 terminated.