无法解析符号'getBoolean'

时间:2017-02-03 11:58:55

标签: android android-activity

我想在第一次用户打开应用程序时显示一些活动,我有代码,但是当我向主要活动添加代码时,它显示错误无法解析符号'getBoolean'< / em>我不知道该怎么做,任何人都可以帮助我吗?

   SharedPreferences preferences=    PreferenceManager.getDefaultSharedPreferences(this);
    if (!preferences.getBoolean("Time",false))
    {

        //stuff here





        SharedPreferences.Editor editor=preferences.edit();
        editor.putBoolean("Time",true);
        editor.commit();
    }

这是我的完整代码`包      com.hackerinside.jaisonjoseph.radioplanet;

 import android.app.AlertDialog;
 import android.app.NotificationManager;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.provider.Settings;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.app.NotificationCompat;
  import android.support.v7.widget.CardView;
 import android.support.v7.widget.Toolbar;
 import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.webkit.WebView;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.ProgressBar;
 import android.widget.Toast;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class MainActivity extends AppCompatActivity {
public ProgressBar bar;



public static boolean isNetworkStatusAvialable (Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null)
    {
        NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
        if(netInfos != null)
            if(netInfos.isConnected())
                return true;
    }
    return false;
}

     SharedPreferences preferences=  PreferenceManager.getDefaultSharedPreferences(this);
     if (!preferences.getBoolean("Time",false))
      {
         //  stuff here




        SharedPreferences.Editor editor=preferences.edit();
       editor.putBoolean("Time",true);
          editor.commit();
     }








@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



   // NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
  //  builder.setSmallIcon(R.drawable.earth);
  //  builder.setContentTitle("RadioPlanet");
   // builder.setContentText("Touch here to stop");
    //int mll=001;
   // NotificationManager mNotification=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
   // mNotification.notify(mll,builder.build());





   final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
   fab.setOnClickListener(new View.OnClickListener() {
     @Override
      public void onClick(View view) {


         if(isNetworkStatusAvialable (getApplicationContext())) {

             Snackbar.make(view, "Please wait and press play button", Snackbar.LENGTH_LONG)
                     .setAction("Action", null).show();

             Radio radio = new Radio();
             android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
             // manager.beginTransaction().replace(android.R.id.content, radio,radio.getTag()).commit();
             manager.beginTransaction().replace(R.id.Relative, radio, radio.getTag()).commit();
             CardView cardView = (CardView) findViewById(R.id.card);
             cardView.setVisibility(View.GONE);
             fab.setVisibility(View.GONE);
             CardView cardView1 = (CardView) findViewById(R.id.card1);
             cardView1.setVisibility(View.GONE);
             CardView cardView2 = (CardView) findViewById(R.id.card2);
             cardView2.setVisibility(View.GONE);

         }
         else {



             Toast.makeText(getApplicationContext(), "Lol , Just turn on your data ", Toast.LENGTH_LONG).show();
         }




      }
  });




  //  final Button button=(Button)findViewById(R.id.button);
//    button.setOnClickListener(new View.OnClickListener() {
       // @Override
        //public void onClick(View v) {
         //   Radio radio=new Radio();
          //  android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
           //  manager.beginTransaction().replace(android.R.id.content, radio,radio.getTag()).commit();
        //    manager.beginTransaction().replace(R.id.Relative, radio,radio.getTag()).commit();
         //   CardView cardView=(CardView)findViewById(R.id.card);
         //  cardView.setVisibility(View.GONE);
         //   button.setVisibility(View.GONE);
         //   CardView cardView1=(CardView)findViewById(R.id.card1);
         //   cardView1.setVisibility(View.GONE);
     //   }
    //});
}




@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction()==KeyEvent.ACTION_DOWN)        {

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                WebView webView = (WebView) findViewById(R.id.webView);
                if (webView != null) {


                    if (webView.canGoBack()) {
                        webView.goBack();
                    } else {
                        finish();
                    }
                    return true;
                }

        }
    }
    return super.onKeyDown(keyCode, event);
}










@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_askme) {


        Intent intent=new Intent("com.hackerinside.jaisonjoseph.radioplanet.Askme");
        startActivity(intent);
        // return true;
    }
    if (id == R.id.action_settings) {


        Intent intent=new Intent("com.hackerinside.jaisonjoseph.radioplanet.Contact");
        startActivity(intent);
       // return true;
    }
    if (id == R.id.action_exit) {


        AlertDialog.Builder alertdl=new AlertDialog.Builder(MainActivity.this);
        alertdl.setMessage("Are you sure you want to exit ?").setCancelable(false);

        alertdl.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {


                finish();
                System.exit(0);



            }
        });
        alertdl.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alertdl.show();

    }
    return super.onOptionsItemSelected(item);
}
}

`

2 个答案:

答案 0 :(得分:1)

    import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class MainActivity extends AppCompatActivity {
    public ProgressBar bar;


    public static boolean isNetworkStatusAvialable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
            if (netInfos != null)
                if (netInfos.isConnected())
                    return true;
        }
        return false;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        if (!(preferences.getBoolean("Time", false))) {
            //  stuff here


            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("Time", true);
            editor.commit();
        }

        // NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
        //  builder.setSmallIcon(R.drawable.earth);
        //  builder.setContentTitle("RadioPlanet");
        // builder.setContentText("Touch here to stop");
        //int mll=001;
        // NotificationManager mNotification=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        // mNotification.notify(mll,builder.build());


        final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (isNetworkStatusAvialable(getApplicationContext())) {

                    Snackbar.make(view, "Please wait and press play button", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();

                    Radio radio = new Radio();
                    android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
                    // manager.beginTransaction().replace(android.R.id.content, radio,radio.getTag()).commit();
                    manager.beginTransaction().replace(R.id.Relative, radio, radio.getTag()).commit();
                    CardView cardView = (CardView) findViewById(R.id.card);
                    cardView.setVisibility(View.GONE);
                    fab.setVisibility(View.GONE);
                    CardView cardView1 = (CardView) findViewById(R.id.card1);
                    cardView1.setVisibility(View.GONE);
                    CardView cardView2 = (CardView) findViewById(R.id.card2);
                    cardView2.setVisibility(View.GONE);

                } else {


                    Toast.makeText(getApplicationContext(), "Lol , Just turn on your data ", Toast.LENGTH_LONG).show();
                }


            }
        });


        //  final Button button=(Button)findViewById(R.id.button);
//    button.setOnClickListener(new View.OnClickListener() {
        // @Override
        //public void onClick(View v) {
        //   Radio radio=new Radio();
        //  android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
        //  manager.beginTransaction().replace(android.R.id.content, radio,radio.getTag()).commit();
        //    manager.beginTransaction().replace(R.id.Relative, radio,radio.getTag()).commit();
        //   CardView cardView=(CardView)findViewById(R.id.card);
        //  cardView.setVisibility(View.GONE);
        //   button.setVisibility(View.GONE);
        //   CardView cardView1=(CardView)findViewById(R.id.card1);
        //   cardView1.setVisibility(View.GONE);
        //   }
        //});
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {

            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    WebView webView = (WebView) findViewById(R.id.webView);
                    if (webView != null) {


                        if (webView.canGoBack()) {
                            webView.goBack();
                        } else {
                            finish();
                        }
                        return true;
                    }

            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_askme) {


            Intent intent = new Intent("com.hackerinside.jaisonjoseph.radioplanet.Askme");
            startActivity(intent);
            // return true;
        }
        if (id == R.id.action_settings) {


            Intent intent = new Intent("com.hackerinside.jaisonjoseph.radioplanet.Contact");
            startActivity(intent);
            // return true;
        }
        if (id == R.id.action_exit) {


            AlertDialog.Builder alertdl = new AlertDialog.Builder(MainActivity.this);
            alertdl.setMessage("Are you sure you want to exit ?").setCancelable(false);

            alertdl.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {


                    finish();
                    System.exit(0);


                }
            });
            alertdl.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            alertdl.show();

        }
        return super.onOptionsItemSelected(item);
    }
}

您的sharedpreference代码未包含在导致问题的任何函数中

答案 1 :(得分:1)

你是否首先存储了布尔变量,如果它使应用程序崩溃它将是由于没有存储的值,你必须首先存储值