当我运行我的应用程序时,它是Crush,那不是Java错误,但是它仍然是Crush并给我错误msg LogFile
我的AddReminderActivity.java
文件
public class AddReminderActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener,
DatePickerDialog.OnDateSetListener, LoaderManager.LoaderCallbacks<Cursor> {
private static final int EXISTING_VEHICLE_LOADER = 0;
private Toolbar mToolbar;
private EditText mTitleText;
private TextView mDateText, mTimeText, mRepeatText, mRepeatNoText, mRepeatTypeText;
private FloatingActionButton mFAB1;
private FloatingActionButton mFAB2 = (FloatingActionButton) findViewById(R.id.starred2);
private Calendar mCalendar;
private int mYear, mMonth, mHour, mMinute, mDay;
private long mRepeatTime;
private Switch mRepeatSwitch;
private String mTitle;
private String mTime;
private String mDate;
private String mRepeat;
private String mRepeatNo;
private String mRepeatType;
private String mActive;
private Uri mCurrentReminderUri;
private boolean mVehicleHasChanged = false;
// Values for orientation change
private static final String KEY_TITLE = "title_key";
private static final String KEY_TIME = "time_key";
private static final String KEY_DATE = "date_key";
private static final String KEY_REPEAT = "repeat_key";
private static final String KEY_REPEAT_NO = "repeat_no_key";
private static final String KEY_REPEAT_TYPE = "repeat_type_key";
private static final String KEY_ACTIVE = "active_key";
// Constant values in milliseconds
private static final long milMinute = 60000L;
private static final long milHour = 3600000L;
private static final long milDay = 86400000L;
private static final long milWeek = 604800000L;
private static final long milMonth = 2592000000L;
private View.OnTouchListener mTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mVehicleHasChanged = true;
return false;
}
};
这是LogFile
消息
2019-07-09 14:54:00.947 8530-8530/com.delaroystudios.alarmreminder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.delaroystudios.alarmreminder, PID: 8530
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.delaroystudios.alarmreminder/com.example.ghazalitodo.AddReminderActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.ghazalitodo.AddReminderActivity.<init>(AddReminderActivity.java:51)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
答案 0 :(得分:0)
使用findViewById
设置布局后,您需要在mFAB2
方法中为onCreate
调用setContentView
。
更改
private FloatingActionButton mFAB2 = (FloatingActionButton) findViewById(R.id.starred2);
到
private FloatingActionButton mFAB2;
在您的onCreate
方法中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(YOUR_LAYOUT); // <- set the layout here
mAddReminderButton = findViewById(R.id.fab); // <- use findViewById here after setContentView
// rest of the code