Sudden error about not resolving some methods and symbols

Viewed 31

I'm a beginner programmer and I'm working on an app where I want to have a Settings activity with 2 switch buttons (one to turn on/off notifications in status bar and one to turn on/off notification sound).Yesterday everything went fine with the compiling but today suddenly I got all these errors about not resolving some methods or symbols. What happened and how can I fix it?

In particular the errors where the following:

  • cannot resolve method: onCreate,setContentView,getSystemService, findViewById
  • cannot resolve symbol: OnCheckedChangeListener, simpleswitch1, simpleswitch2, arg0, view
  • Override: Annotations are not allowed here
  • onClick and onCheckedChanged do not work properly.
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.annotation.RequiresApi;

import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;


public class Settings extends AppCompatActivity {
    Switch simpleSwitch1, simpleSwitch2;
    NotificationManager manager;
    Notification myNotication;
    private Notification notification;

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



        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
        simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
        simpleSwitch1.setOnCheckedChangeListener(new View.OnCheckedChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            public void onCheckedChanged(CompoundButton simpleswitch1, boolean isChecked) {
        if (isChecked) {


                @Override
                public void onClick(View arg0) {
                    //API level 11
                    Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");

                    PendingIntent pendingIntent = PendingIntent.getActivity(Settings.this, 1, intent, 0);

                    Notification.Builder builder = new Notification.Builder(Settings.this);

                    builder.setAutoCancel(false);
                    builder.setTicker("this is ticker text");
                    builder.setContentTitle("WhatsApp Notification");
                    builder.setContentText("You have a new message");
                    builder.setSmallIcon(R.drawable.notification);
                    builder.setContentIntent(pendingIntent);
                    builder.setOngoing(true);
                    builder.setSubText("This is subtext...");   //API level 16
                    builder.setNumber(100);
                    builder.build();

                    myNotication = builder.getNotification();
                    manager.notify(11, myNotication);

                }
            }

        else{
            simpleSwitch1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    manager.cancel(11);
                }
            });
        }

                }});





    public void onClick(View view) {
            public void onCheckedChanged(CompoundButton simpleswitch2, boolean isChecked) {
        if (isChecked){
            notification.defaults |= Notification.DEFAULT_SOUND;




    }}}}}

XML File

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Settings">


    <TextView
        android:id="@+id/textView16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="SETTINGS"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
        android:textColor="@color/design_default_color_primary_dark"
        android:textColorHighlight="@color/design_default_color_primary"
        android:textStyle="bold" />

    <Switch
        android:id="@+id/simpleswitch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView16"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:text="NOTIFICATIONS"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="@color/colorPrimary"
        android:textStyle="normal"
        android:checked="true"
        />

    <Switch
        android:id="@+id/simpleswitch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/simpleswitch1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="SOUNDS"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="@color/colorPrimary"
        android:checked="true" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="61dp"
        android:layout_height="44dp"
        android:layout_marginLeft="280dp"
        android:layout_marginTop="30dp"
        app:srcCompat="@drawable/ic_menu_manage" />

</RelativeLayout>
1 Answers

From you java you are placing methods inside other methods this is not possible in java

Also Try setting listeners as such

  // set default state on/off
    simpleSwitch1.setChecked(false);
    simpleSwitch2.setChecked(false);

    simpleSwitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                // prepare your notification and display
            }else{
                // do something
            }
        }

    });

    simpleSwitch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                // prepare your notification and display
            }else{
                // do something
            }
        }

    });

hope this helps, your java code was quite hard to understand.

Related