gpt4 book ai didi

java - 打开其他布局时,应用已停止

转载 作者:行者123 更新时间:2023-12-02 08:05:51 25 4
gpt4 key购买 nike

我正在创建一个用于计划的应用程序,当我选择一个日期并按select键以移至另一个布局以为此日期创建任务时,该应用程序将停止。

这是我的主要代码,效果很好:

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);
}
});

}
}

这是我的第二项 Activity ,我在其中使用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适配器,我很确定这是有问题的,但是我并没有真正了解问题以及如何解决它-
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;
}
}


任务是一个基本对象;
    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.

最佳答案

变更委托(delegate)人:

 public ScheduleAdapter(@NonNull Context context, int mResource, @NonNull 
ArrayList<Task> tasks) {
super(context,0, tasks);
}

我认为您必须在委托(delegate)方中执行此操作:mContext = context;

因为您的mcontext为null,所以您必须将 Activity 的上下文分配给适配器上下文

关于java - 打开其他布局时,应用已停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60280236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com