我找不到指示的错误:Array Adapter

时间:2015-10-30 12:37:20

标签: android android-activity android-listview android-arrayadapter

有人可以帮我找到我的代码中的错误。在第一个活动中按下按钮时,应用程序从两个文本字段中获取文本并将其插入另一个活动的列表视图中。为此,我将其他活动实现到第一个活动中,以便能够调用其方法。但是,阵列适配器似乎存在问题。

这是第一项活动的代码:

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ReminderSetActivity extends AppCompatActivity {

    PendingReminderActivity pendingReminderActivity = new PendingReminderActivity();

    private static final int uniqueID = 45612;

    EditText titleEditText;
    EditText detailsEditText;
    Button setReminderButton;
    TextView gotoPendingRemindersTextView;
    NotificationCompat.Builder notification;

    String title;
    String details;

    private static boolean isStringValid(String string) {
        if (string == null) {
            return false;
        } else {
            String s = string.trim();
            return !((s.length() == 0) || (s.equalsIgnoreCase("")));
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder_set);


        titleEditText = (EditText) findViewById(R.id.titleEditText);
        titleEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (isStringValid(titleEditText.getText().toString().trim()) &&
                        isStringValid(detailsEditText.getText().toString())) {
                    setReminderButton.setEnabled(true);
                } else {
                    setReminderButton.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        detailsEditText = (EditText) findViewById(R.id.detailsEditText);
        detailsEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (isStringValid(titleEditText.getText().toString().trim()) &&
                        isStringValid(detailsEditText.getText().toString())) {
                    setReminderButton.setEnabled(true);
                } else {
                    setReminderButton.setEnabled(false);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        setReminderButton = (Button) findViewById(R.id.setReminderButton);
        setReminderButton.setEnabled(false);
        setReminderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                title = titleEditText.getText().toString();
                details = detailsEditText.getText().toString();

                pendingReminderActivity.addReminders(title, details);
                pendingReminderActivity.populateList();

                Intent intent = new Intent(ReminderSetActivity.this, PendingReminderActivity.class);

                notification = new NotificationCompat.Builder(getApplicationContext());
                notification.setAutoCancel(true);
                notification.setSmallIcon(R.mipmap.ic_launcher);
                notification.setTicker(titleEditText.getText().toString().trim());
                notification.setWhen(System.currentTimeMillis());
                notification.setContentTitle(titleEditText.getText().toString().trim());
                notification.setContentText(detailsEditText.getText().toString());

                PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                notification.setContentIntent(pendingIntent);

                //Builds notification and issues it
                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                nm.notify(uniqueID, notification.build());

            }
        });

        gotoPendingRemindersTextView = (TextView) findViewById(R.id.gotoPendingRemindersTextView);
        gotoPendingRemindersTextView.setPaintFlags(gotoPendingRemindersTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        gotoPendingRemindersTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ReminderSetActivity.this, PendingReminderActivity.class));
            }
        });

    }

这是第二项活动的代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class PendingReminderActivity extends AppCompatActivity {

    public List<Reminder> reminders = new ArrayList<>();
    TextView titleTextView;
    TextView detailsTextView;
    ListView reminderListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pending_reminder);

        titleTextView = (TextView) findViewById(R.id.titleTextView);
        detailsTextView = (TextView) findViewById(R.id.detailsTextView);

        reminderListView = (ListView) findViewById(R.id.pendingRemindersListView);

    }

    public void addReminders(String title, String details) {
        reminders.add(new Reminder(title, details));
    }

    public void populateList() {
        ArrayAdapter<Reminder> reminderArrayAdapter = new ReminderListAdapter();
        reminderListView.setAdapter(reminderArrayAdapter);
    }

    private class ReminderListAdapter extends ArrayAdapter<Reminder> {
        public ReminderListAdapter() {
            super(PendingReminderActivity.this, R.layout.listview_item, reminders);
        }


        @Override
        public View getView(int pos, View view, ViewGroup parent) {

            if (view == null)
                view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);

            Reminder pendingReminder = reminders.get(pos);

            titleTextView = (TextView) view.findViewById(R.id.titleTextView);
            titleTextView.setText(pendingReminder.getTitle());
            detailsTextView = (TextView) view.findViewById(R.id.detailsTextView);
            detailsTextView.setText(pendingReminder.getDetails());

            return view;
        }
    }
}

这是日志:

10-30 14:24:25.552 27155-27155/? I/art: Late-enabling -Xcheck:jni
10-30 14:24:25.601 27155-27155/com.bakedbeandevelopment.rememberme W/ResourceType: Found multiple library tables, ignoring...
10-30 14:24:25.695 27155-27185/com.bakedbeandevelopment.rememberme D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-30 14:24:25.700 27155-27155/com.bakedbeandevelopment.rememberme D/Atlas: Validating map...
10-30 14:24:25.732 27155-27185/com.bakedbeandevelopment.rememberme I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.01.00.042.030_msm8974_LA.BF.1.1.1_RB1__release_AU ()
10-30 14:24:25.732 27155-27185/com.bakedbeandevelopment.rememberme I/Adreno-EGL: OpenGL ES Shader Compiler Version: E031.25.03.06
10-30 14:24:25.732 27155-27185/com.bakedbeandevelopment.rememberme I/Adreno-EGL: Build Date: 05/17/15 Sun
10-30 14:24:25.732 27155-27185/com.bakedbeandevelopment.rememberme I/Adreno-EGL: Local Branch: mybranch10089422
10-30 14:24:25.732 27155-27185/com.bakedbeandevelopment.rememberme I/Adreno-EGL: Remote Branch: quic/LA.BF.1.1.1_rb1.22
10-30 14:24:25.732 27155-27185/com.bakedbeandevelopment.rememberme I/Adreno-EGL: Local Patches: NONE
10-30 14:24:25.732 27155-27185/com.bakedbeandevelopment.rememberme I/Adreno-EGL: Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.01.00.042.030 + 6151be1 +  NOTHING
10-30 14:24:25.733 27155-27185/com.bakedbeandevelopment.rememberme I/OpenGLRenderer: Initialized EGL, version 1.4
10-30 14:24:25.746 27155-27185/com.bakedbeandevelopment.rememberme D/OpenGLRenderer: Enabling debug mode 0
10-30 14:24:25.789 27155-27155/com.bakedbeandevelopment.rememberme I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3921cd84 time:2101262
10-30 14:24:32.953 27155-27155/com.bakedbeandevelopment.rememberme D/AndroidRuntime: Shutting down VM
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime: FATAL EXCEPTION: main
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime: Process: com.bakedbeandevelopment.rememberme, PID: 27155
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime: java.lang.IllegalStateException: System services not available to Activities before onCreate()
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at android.app.Activity.getSystemService(Activity.java:5035)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at com.bakedbeandevelopment.rememberme.PendingReminderActivity$ReminderListAdapter.<init>(PendingReminderActivity.java:37)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at com.bakedbeandevelopment.rememberme.PendingReminderActivity.populateList(PendingReminderActivity.java:63)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at com.bakedbeandevelopment.rememberme.ReminderSetActivity$3.onClick(ReminderSetActivity.java:107)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at android.view.View.performClick(View.java:4856)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19956)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:211)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5373)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
10-30 14:24:32.955 27155-27155/com.bakedbeandevelopment.rememberme E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
10-30 14:25:09.652 27155-27155/com.bakedbeandevelopment.rememberme I/Process: Sending signal. PID: 27155 SIG: 9

请有人向我解释这个错误,如果可能的话,建议我该如何解决它。

提前致谢。

2 个答案:

答案 0 :(得分:1)

onCreate()之外声明列表并在onCreate()

内实例化
public List<Reminder> reminders;

在onCreate中:

reminders = new ArrayList<>();

答案 1 :(得分:1)

将以下行更改为onCreate():

PendingReminderActivity pendingReminderActivity = new PendingReminderActivity();

只需在onCreate()中声明PendingReminderActivity,如下所示:

PendingReminderActivity pendingReminderActivity;

并在onCreate()中启动它。