java.lang.nullpointerexception错误

时间:2014-07-16 12:23:14

标签: android nullpointerexception

我对android很新,请不要介意这个愚蠢的问题,但如果你知道的话,请回答它!!! 当我使用intent开始一个新活动时,但是当我开始新活动时,我的应用程序无法正常停止。错误是java.lang.NullPointerException。 我浪费了很多时间,我无法理解问题所在。

这是第一项活动:

package com.people.rock;

import com.android.taskreminder.R;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ReminderListActivity extends ListActivity {

    private static final int ACTIVITY_CREATE=0;
    //private static final int ACTIVITY_EDIT=1;

    private RemindersDbAdapter mDbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder_list);

        mDbHelper = new RemindersDbAdapter(this);
        mDbHelper.open();
        fillData();
        registerForContextMenu(getListView());

        }
    @SuppressWarnings("deprecation")
    private void fillData() {
        Cursor remindersCursor = mDbHelper.fetchAllReminders(); 
        startManagingCursor(remindersCursor); 
        // Create an array to specify the fields we want (only the TITLE)
        String[] from = new String[]{RemindersDbAdapter.KEY_TITLE}; 
        // and an array of the fields we want to bind in the view
        int[] to = new int[]{R.id.text1}; 
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter reminders =
        new SimpleCursorAdapter(this, R.layout.reminder_row, remindersCursor, from, to);
        setListAdapter(reminders); 
        }



    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, ReminderEditActivity.class); 
    i.putExtra("RowId", id);
    startActivity(i);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo
    menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.list_menu_item_longpress, menu);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.reminder_list, menu);
        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.list_menu, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) { 
        switch(item.getItemId()) { 
        case R.id.menu_insert:
            createReminder();
            return true; 
    }
    return super.onMenuItemSelected(featureId, item);
    }

    public boolean onContextItemSelected(MenuItem item) { 
        switch(item.getItemId()) { 
        case R.id.menu_delete:
            AdapterContextMenuInfo info =   (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteReminder(info.id); 
            fillData(); 
            return true; 
    }
    return super.onContextItemSelected(item);
    }


    private void createReminder() {
    Intent i = new Intent(this, ReminderEditActivity.class);
    startActivityForResult(i, ACTIVITY_CREATE);
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
    super.onActivityResult(requestCode, resultCode, intent);
     fillData();
    }



}

这是我使用intent调用的新活动。

package com.people.rock;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;

    import com.android.taskreminder.R;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.app.TimePickerDialog;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.TimePicker;
    import android.widget.Toast;

    @SuppressLint("SimpleDateFormat") public class ReminderEditActivity extends Activity {

        // all variables are declared before content view and are initiated after it.
        private Button mTimeButton;
        private Button mDateButton;
        private static final String DATE_FORMAT = "yyyy-MM-dd";
        private static final String TIME_FORMAT = "hour-minute";
        public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";
        private static final int DATE_PICKER_DIALOG = 0;
        private static final int TIME_PICKER_DIALOG = 1;
        Calendar mCalendar;
        private EditText mTitleText;
        private Button mConfirmButton;
        private EditText mBodyText;
        private RemindersDbAdapter mDbHelper;
        private Long mRowId;

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

        registerButtonListnerAndSetDefaultText();

        //variable initiation
            mDbHelper = new RemindersDbAdapter(this);
            mDateButton=(Button) findViewById(R.id.reminder_date);
            mTimeButton=(Button) findViewById(R.id.reminder_time);

         mCalendar = Calendar.getInstance();

         mConfirmButton = (Button) findViewById(R.id.confirm);

         mTitleText = (EditText) findViewById(R.id.title);

         mBodyText = (EditText) findViewById(R.id.body);

         //to check whether any previous state exist which might be closed by killing of activity
         mRowId = savedInstanceState != null 
                 ? savedInstanceState.getLong(RemindersDbAdapter.KEY_ROWID)
                 : null;

        }

        //to set id if we got it from intent declared in reminder_list activity
        private void setRowIdFromIntent() { 
            if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null
            ? extras.getLong(RemindersDbAdapter.KEY_ROWID)
            : null;
            }
            }

            @Override
            protected void onPause() {
            super.onPause();
            mDbHelper.close(); 
            }

            @Override
            protected void onResume() { 
            super.onResume();
            mDbHelper.open(); 
            setRowIdFromIntent(); 
            populateFields(); 
            }

            //to fill the form of existing id which was killed by android
            @SuppressWarnings("deprecation")
            private void populateFields() { 
            if (mRowId != null) {
            Cursor reminder = mDbHelper.fetchReminder(mRowId); 
            startManagingCursor(reminder); 
            mTitleText.setText(reminder.getString(
            reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_TITLE))); 
            mBodyText.setText(reminder.getString(
            reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_BODY))); 
            SimpleDateFormat dateTimeFormat =   new SimpleDateFormat(DATE_TIME_FORMAT); 
            Date date = null; 
            try {
            String dateString = reminder.getString(
            reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_DATE_TIME)); 
            date = dateTimeFormat.parse(dateString); 
            mCalendar.setTime(date); 
            } catch (ParseException e) {
                Log.e("ReminderEditActivity", e.getMessage(), e); 
            }
            }
            updateDateButtonText();
            updateTimeButtonText();
            }

            @Override
            protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putLong(RemindersDbAdapter.KEY_ROWID, mRowId);
            }

                private void registerButtonListnerAndSetDefaultText(){
            mConfirmButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                saveState(); 
                setResult(RESULT_OK); 
                Toast.makeText(ReminderEditActivity.this,getString(R.string.task_saved_message),Toast.LENGTH_SHORT).show();
                finish();
                }
                });
            mDateButton.setOnClickListener(new View.OnClickListener() { 
                    @SuppressWarnings("deprecation")
                    @Override           
                public void onClick(View v) { 
                    showDialog(DATE_PICKER_DIALOG); 
                }
                });
            mTimeButton.setOnClickListener(new View.OnClickListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void onClick(View v) {
                    showDialog(TIME_PICKER_DIALOG);
                }
            });
                updateDateButtonText(); 
                updateTimeButtonText();
        }

        @SuppressWarnings("deprecation")
        protected Dialog onCreateDialog(int id) { 
                switch(id) {
           case DATE_PICKER_DIALOG:
               return showDatePicker();
           case TIME_PICKER_DIALOG:
               return showTimePicker();
        }
        return super.onCreateDialog(id);
        }

        @SuppressLint("SimpleDateFormat") private DatePickerDialog showDatePicker() { 
        DatePickerDialog datePicker = new DatePickerDialog(ReminderEditActivity.this ,new DatePickerDialog.OnDateSetListener() { 
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,   int dayOfMonth) { 

               mCalendar.set(Calendar.YEAR, year); 
               mCalendar.set(Calendar.MONTH, monthOfYear);
               mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                       updateDateButtonText(); 
        }
        }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
              return datePicker; 
        }
        private void updateDateButtonText() { 
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 
        String dateForButton = dateFormat.format(mCalendar.getTime()); 
        mDateButton.setText(dateForButton); 
        }

        private TimePickerDialog showTimePicker() {
            TimePickerDialog timePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { 
            @SuppressLint("SimpleDateFormat") @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute){ 
            mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay); 
            mCalendar.set(Calendar.MINUTE, minute); 
            updateTimeButtonText(); 
            }
            }, mCalendar.get(Calendar.HOUR_OF_DAY), 
            mCalendar.get(Calendar.MINUTE), true); 
            return timePicker;
            }
        @SuppressLint("SimpleDateFormat") private void updateTimeButtonText() { 
            SimpleDateFormat TimeFormat = new SimpleDateFormat(TIME_FORMAT); 
            String timeForButton = TimeFormat.format(mCalendar.getTime()); 
            mDateButton.setText(timeForButton); 
            }

        private void saveState() {
            String title = mTitleText.getText().toString(); 
            String body = mBodyText.getText().toString();
            SimpleDateFormat dateTimeFormat = new   SimpleDateFormat(DATE_TIME_FORMAT);
            String reminderDateTime = dateTimeFormat.format(mCalendar.getTime());
            if (mRowId == null) { 
                long id = mDbHelper.createReminder(title, body, reminderDateTime);

                if (id > 0) {
                mRowId = id; 
                }
                } else {
                mDbHelper
                .updateReminder(mRowId, title, body, reminderDateTime);
                }
            new ReminderManager(this).setReminder(mRowId, mCalendar);
                }
                    }

这就是我得到的错误

07-16 08:04:30.620: D/dalvikvm(945): GC_FOR_ALLOC freed 44K, 4% free 3054K/3168K, paused 35ms, total 38ms
07-16 08:04:30.630: I/dalvikvm-heap(945): Grow heap (frag case) to 3.520MB for 500416-byte allocation
07-16 08:04:30.670: D/dalvikvm(945): GC_FOR_ALLOC freed 2K, 4% free 3540K/3660K, paused 33ms, total 33ms
07-16 08:04:31.570: I/Choreographer(945): Skipped 38 frames!  The application may be doing too much work on its main thread.
07-16 08:04:31.800: D/gralloc_goldfish(945): Emulator without GPU emulation detected.
07-16 08:11:32.195: D/dalvikvm(945): GC_FOR_ALLOC freed 56K, 4% free 3717K/3840K, paused 108ms, total 109ms
07-16 08:11:32.195: I/dalvikvm-heap(945): Grow heap (frag case) to 4.142MB for 474384-byte allocation
07-16 08:11:32.425: D/dalvikvm(945): GC_FOR_ALLOC freed 13K, 4% free 4167K/4304K, paused 53ms, total 53ms
07-16 08:11:32.685: D/AndroidRuntime(945): Shutting down VM
07-16 08:11:32.685: W/dalvikvm(945): threadid=1: thread exiting with uncaught exception (group=0xb3a62ba8)
07-16 08:11:32.715: E/AndroidRuntime(945): FATAL EXCEPTION: main
07-16 08:11:32.715: E/AndroidRuntime(945): Process: com.android.taskreminder, PID: 945
07-16 08:11:32.715: E/AndroidRuntime(945): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.taskreminder/com.people.rock.ReminderEditActivity}: java.lang.NullPointerException
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.os.Looper.loop(Looper.java:136)
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 08:11:32.715: E/AndroidRuntime(945):  at java.lang.reflect.Method.invokeNative(Native Method)
07-16 08:11:32.715: E/AndroidRuntime(945):  at java.lang.reflect.Method.invoke(Method.java:515)
07-16 08:11:32.715: E/AndroidRuntime(945):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 08:11:32.715: E/AndroidRuntime(945):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 08:11:32.715: E/AndroidRuntime(945):  at dalvik.system.NativeStart.main(Native Method)
07-16 08:11:32.715: E/AndroidRuntime(945): Caused by: java.lang.NullPointerException
07-16 08:11:32.715: E/AndroidRuntime(945):  at com.people.rock.ReminderEditActivity.registerButtonListnerAndSetDefaultText(ReminderEditActivity.java:121)
07-16 08:11:32.715: E/AndroidRuntime(945):  at com.people.rock.ReminderEditActivity.onCreate(ReminderEditActivity.java:47)
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.app.Activity.performCreate(Activity.java:5231)
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-16 08:11:32.715: E/AndroidRuntime(945):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-16 08:11:32.715: E/AndroidRuntime(945):  ... 11 more
07-16 08:11:36.505: I/Process(945): Sending signal. PID: 945 SIG: 9

提前致谢

3 个答案:

答案 0 :(得分:1)

在初始化之前,您正在mConfirmButton,mDateButton,mTimeButton函数中使用按钮侦听器registerButtonListnerAndSetDefaultText())。

在将按钮初始化为

后调用registerButtonListnerAndSetDefaultText()
     mDbHelper = new RemindersDbAdapter(this);
     mDateButton=(Button) findViewById(R.id.reminder_date);
     mTimeButton=(Button) findViewById(R.id.reminder_time);
     mCalendar = Calendar.getInstance();
     mConfirmButton = (Button) findViewById(R.id.confirm);
     mTitleText = (EditText) findViewById(R.id.title);
     mBodyText = (EditText) findViewById(R.id.body);

     registerButtonListnerAndSetDefaultText();  // use here

答案 1 :(得分:0)

你的失败是你在id找到按钮mConfirmButton之前调用了registerButtonListnerAndSetDefaultText();)

答案 2 :(得分:0)

在初始化registerButtonListnerAndSetDefaultText();之前,您正在调用mConfirmButton,因此它会提供nullpointerexception。