通过意图获取数据并将其显示在列表视图中时,应用程序正在停止
Posted
技术标签:
【中文标题】通过意图获取数据并将其显示在列表视图中时,应用程序正在停止【英文标题】:App is getting stopped when getting data through intent and displaying it in listview 【发布时间】:2020-08-12 13:46:59 【问题描述】:我正在开发一个应用程序,我将数据从一个活动发送到另一个活动并在 Listview 中显示。
我的代码没有错误,我也检查了 Logcat 错误,但没有任何错误,我的应用程序正在关闭。
我已附上代码供参考。
这是Reminder.java
提醒.java
public class Reminder extends AppCompatActivity
ListView listView;
TextView custom_reminder;
ArrayList<String> reminders;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
getSupportActionBar().setTitle("Reminders");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
custom_reminder = (TextView) findViewById(R.id.textView3);
listView = (ListView) findViewById(R.id.listView);
reminders = new ArrayList<>();
adapter = new ArrayAdapter<>(Reminder.this, R.layout.reminder_row, R.id.reminder, reminders);
custom_reminder.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(Reminder.this, WorkReminder.class);
startActivity(intent);
);
String reminderName = getIntent().getStringExtra("REMINDER_NAME");
reminders.add(reminderName);
listView.setAdapter(adapter);
这是 WorkReminder.java
WorkReminder.java
public class WorkReminder extends AppCompatActivity
private int notificationId = 1;
EditText reminder_text;
TimePicker timePicker;
Button setReminder, cancel;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_reminder);
getSupportActionBar().setTitle("Create reminders");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
createNotificationChannel();
reminder_text = (EditText) findViewById(R.id.reminder_text);
timePicker = (TimePicker) findViewById(R.id.timePicker);
setReminder = (Button) findViewById(R.id.set_button);
cancel = (Button) findViewById(R.id.cancel_button);
setReminder.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (reminder_text.getText().toString().isEmpty())
reminder_text.setError("Fields can't be empty.");
reminder_text.setFocusable(true);
else
Toast.makeText(WorkReminder.this, "Reminder Set!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(WorkReminder.this, AlarmReceiver.class);
intent.putExtra("notificationId", notificationId);
intent.putExtra("todo", reminder_text.getText().toString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(WorkReminder.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
// Create time.
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, hour);
startTime.set(Calendar.MINUTE, minute);
startTime.set(Calendar.SECOND, 0);
long alarmStartTime = startTime.getTimeInMillis();
// Set alarm.
// set(type, milliseconds, intent)
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStartTime, pendingIntent);
String reminder_name = reminder_text.getText().toString();
String time = hour + ":" + minute;
Intent intent1 = new Intent(WorkReminder.this, Reminder.class);
intent1.putExtra("REMINDER_NAME", reminder_name);
intent1.putExtra("TIME", time);
startActivity(intent1);
);
cancel.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(WorkReminder.this, "Reminder cancelled!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(WorkReminder.this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(WorkReminder.this, 0, intent, 0);
alarmManager.cancel(pendingIntent);
);
private void createNotificationChannel()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
CharSequence title = "Reminder!";
String description = "test";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("reminder", title, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
reminder_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/reminder"
android:layout_
android:layout_
android:text="Main Title"
android:textColor="#000"
android:textStyle="bold"
android:layout_margin="0dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/time"
android:layout_
android:layout_
android:text="Time"
android:textColor="#000"
android:textStyle="bold"
android:layout_marginTop="3dp"
android:textSize="16sp"/>
</LinearLayout>
Logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
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.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
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 androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
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)
我要做的是从 WorkReminder.java 获取提醒数据并通过 Intent 将其发送到 Reminder.java。而在 Reminder.java 中,我使用 listView 来显示相同的数据。
用户每次提醒时,列表视图都应该添加它。
【问题讨论】:
【参考方案1】:您将“null”值放入数组适配器字符串。请在将它添加到数组之前检查 null 提醒名称。
String reminderName = getIntent().getStringExtra("REMINDER_NAME");
if (reminderName != null)
reminders.add(reminderName);
adapter = new ArrayAdapter<>(Reminder.this, R.layout.reminder_row, R.id.reminder, reminders);
listView.setAdapter(adapter);
【讨论】:
以上是关于通过意图获取数据并将其显示在列表视图中时,应用程序正在停止的主要内容,如果未能解决你的问题,请参考以下文章
通过 URL 从 JSON 数据中获取数据并使用列表视图或回收器视图将其显示在 android 应用程序中
当用户旋转手机并将所有数据保留在 ArrayAdapter 中时,保留列表视图项的良好解决方案
通过 REST API 检索结果并将其显示在新活动的列表视图中(列表视图出现小问题)