关于不解决某些方法和符号的突然错误

问题描述 投票:2回答:1

我是一名初学者,正在开发一个应用程序,该应用程序具有一个带有2个开关按钮的设置活动(一个用于打开/关闭状态栏中的通知,另一个用于打开/关闭通知声音)。昨天编译时一切正常,但是今天突然我收到了所有关于不解析某些方法或符号的错误。发生了什么事,我该如何解决?

特别是以下错误:

  • 无法解析方法:onCreate,setContentView,getSystemService,findViewById
  • 无法解析符号:OnCheckedChangeListener,simpleswitch1,simpleswitch2,arg0,视图
  • 替代:此处不允许注释
  • onClick和onCheckedChanged无法正常工作。
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文件

<?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>
java notifications switch-statement
1个回答
0
投票

从您的Java中将方法放置在其他方法中,这在Java中是不可能的

也尝试像这样设置侦听器

  // 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
            }
        }

    });

希望这会有所帮助,您的Java代码很难理解。

© www.soinside.com 2019 - 2024. All rights reserved.