-2

pls help

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference at com.example.c1.firebaselogin.ClockActivity.onCreate(ClockActivity.java:61)

AlarmManager alarmManager;
private PendingIntent pending_intent;

private TimePicker alarmTimePicker;
private TextView alarmTextView;

private AlarmReceiver alarm;


ClockActivity inst;
Context context;

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



    this.context = this;

    //alarm = new AlarmReceiver();
    alarmTextView = (TextView) findViewById(R.id.alarmText);

    final Intent myIntent = new Intent(this.context, AlarmReceiver.class);

    // Get the alarm manager service
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    // set the alarm to the time that you picked
    final Calendar calendar = Calendar.getInstance();

    alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);



    Button startalarm;
    startalarm = (Button)findViewById(R.id.start_alarm);
    startalarm.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.M)

        @Override
        public void onClick(View v) {

            calendar.add(Calendar.SECOND, 3);
            //setAlarmText("You clicked a button");

            final int hour = alarmTimePicker.getCurrentHour();
            final int minute = alarmTimePicker.getCurrentMinute();;

            Log.e("MyActivity", "In the receiver with " + hour + " and " + minute);
            setAlarmText("You clicked a " + hour + " and " + minute);


            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());

            myIntent.putExtra("extra", "yes");
            pending_intent = PendingIntent.getBroadcast(ClockActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);


            // now you should change the set Alarm text so it says something nice


            setAlarmText("Alarm set to " + hour + ":" + minute);
            //Toast.makeText(getApplicationContext(), "You set the alarm", Toast.LENGTH_SHORT).show();
        }

    });

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


            int min = 1;
            int max = 9;

            Random r = new Random();
            int random_number = r.nextInt(max - min + 1) + min;
            Log.e("random number is ", String.valueOf(random_number));

            myIntent.putExtra("extra", "no");
            sendBroadcast(myIntent);

            alarmManager.cancel(pending_intent);
            setAlarmText("Alarm canceled");
            //setAlarmText("You clicked a " + " canceled");
        }
    });

}

public void setAlarmText(String alarmText) {
    alarmTextView.setText(alarmText);
}



@Override
public void onStart() {
    super.onStart();
    inst = this;
}

@Override
public void onDestroy() {
    super.onDestroy();

    Log.e("MyActivity", "on Destroy");
}

my line 61 is startalarm = (Button)findViewById(R.id.start_alarm);

and my xml is

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set alarm"
        android:id="@+id/start_alarm"
        android:onClick="startTimer"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/alarmText"
        android:layout_marginLeft="63dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
         />

thank you

  • 4
    That Exception message doesn't line up with the given code. Every `findViewById()` call shown in your code is being called on `ClockActivity`, but the Exception says you're calling `findViewById()` on a `TextView` somewhere. Are you sure that's the current Exception? – Mike M. Nov 20 '18 at 04:23
  • Is your start_alarm xml a button though? – MetaSnarf Nov 20 '18 at 04:23

1 Answers1

-1

I think the problem is in your xml > android:onClick="startTimer" you are saying to your button to call "startTimer" but you have also define work in your java class start_alarm.setOnClick of the Button. Remove that line from xml and I hope it will work. do let me know if it works.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Khalid Saeed
  • 131
  • 2
  • 8