我想在用户第一次打开应用程序时显示一些活动,我有相应的代码,但是当我向主活动添加代码时,它显示一个无法解析符号“getboolean”的错误我不知道该怎么办,有人可以帮助我吗?
SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(this);
if (!preferences.getBoolean("Time",false))
{
//stuff here
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean("Time",true);
editor.commit();
}
这是我的完整代码'package
网址: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);
}
}
`
最佳答案
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
代码没有包含在任何函数中..这导致了问题