我有一个主要的活动,我打电话给
VersionSettings vs = new VersionSettings(this);
if (vs.firstRun2())
vs.versionCheckbox();
如果Android版本为1.6-2.1,我正在尝试将复选框(checkboxVideoType)设置为未选中状态
我只想在应用程序第一次运行时执行此操作,它永远不需要再次运行。
我认为当主要活动调用versionCheckbox()时遇到问题,如果尝试运行内部代码将关闭复选框设置为false,则会强制关闭。
我对编程非常陌生,非常感谢对此有所帮助。我想我很接近,但需要推动。提前谢谢!
主要活动
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
public class Nasatv extends Activity implements OnClickListener {
boolean checkboxIsChecked;
SharedPreferences nasaTV_Prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
nasaTV_Prefs = getSharedPreferences("nasaTV_Prefs", 0);
ChangeLog cl = new ChangeLog(this);
if (cl.firstRun())
cl.getLogDialog().show();
VersionSettings vs = new VersionSettings(this);
if (vs.firstRun2())
vs.versionCheckbox();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Nasatv.this, LaunchCalendar.class);
startActivity(i);
}
});
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Nasatv.this, PhotoInfo.class);
startActivity(i);
}
});
CheckConnectivity check = new CheckConnectivity();
Boolean conn = check.checkNow(this.getApplicationContext());
if(conn == true){
ImageView updateImage = (ImageView) findViewById(R.id.updateImage);
ImageDownloader downloader = new ImageDownloader(updateImage);
downloader.execute("http://www.url.com/trl/ubox.jpg");
} else {
ImageView updateImage = (ImageView) findViewById(R.id.updateImage);
updateImage.setImageResource(R.drawable.uboxerror);
}
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.setting_title:
Intent settingsActivity =new Intent(getBaseContext(), Settings.class);
startActivity(settingsActivity);
return true;
case R.id.photo_archive:
Intent archive = new Intent(Nasatv.this, PhotoArchive.class);
startActivity(archive);
return true;
case R.id.n_web:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.nasa.gov/"));
startActivity(intent);
return true;
case R.id.exit_title:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onResume() {
super.onResume();
checkboxIsChecked = nasaTV_Prefs.getBoolean("checkboxVideoType", true);
}
@Override
public void onClick(View v) {
if (checkboxIsChecked) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.nasa.gov/multimedia/nasatv/nasatv_android_flash.html"));
startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("rtsp://nasadln.qt.llnwd.net/nasa101.sdp"));
startActivity(intent);
}
}
}
一次性跑步
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.CheckBox;
public class VersionSettings {
private final Context context;
private String notRun, hasRun;
private SharedPreferences run;
private CheckBox checkboxVideoType;
private SharedPreferences nasaTV_Prefs;
private static final String HAS_RUN = "PREFS_HAS_RUN";
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
/**
* Constructor
*
* Retrieves whether the app has been run or not and saves to
* SharedPreferences
*/
public VersionSettings(Context context) {
this.context = context;
this.run = PreferenceManager.getDefaultSharedPreferences(context);
// get run/not run string number, which is "1"
this.notRun = run.getString(HAS_RUN, "");
Log.d(TAG, "notRun: " + notRun);
this.hasRun = context.getResources().getString(R.string.has_run_string);
Log.d(TAG, "hasRun: " + hasRun);
// save new number to preferences, which will be the same number,
// so this is run only the very first time the app is run
SharedPreferences.Editor editor = run.edit();
editor.putString(HAS_RUN, hasRun);
editor.commit();
}
/**
* @return true if this version of your app is started for the first
* time
*/
public boolean firstRun2() {
return ! notRun.equals(hasRun);
}
/**
* @return Change the checkboxVideoType to "unchecked" (false)
*
*/
public void versionCheckbox() {
// this.context = context;
if (currentapiVersion < android.os.Build.VERSION_CODES.FROYO){
this.nasaTV_Prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = nasaTV_Prefs.edit();
editor.putBoolean("checkboxVideoType", false);
editor.commit();
}
}
private static final String TAG = "VersionSettings";
}
偏好活动
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class Settings extends Activity implements OnCheckedChangeListener {
private CheckBox checkboxVideoType;
private SharedPreferences nasaTV_Prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.preferences);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
checkboxVideoType = (CheckBox) findViewById(R.id.checkboxVideoType);
checkboxVideoType.setOnCheckedChangeListener(this);
nasaTV_Prefs = getSharedPreferences("nasaTV_Prefs", 0);
checkboxVideoType.setChecked(nasaTV_Prefs.getBoolean("checkboxVideoType", true));
Button clbutton = (Button) findViewById(R.id.clbutton);
clbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ChangeLog cl = new ChangeLog(Settings.this);
cl.getFullLogDialog().show();
}
});
}
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
if (cb == checkboxVideoType){
SharedPreferences.Editor editor = nasaTV_Prefs.edit();
editor.putBoolean("checkboxVideoType", isChecked);
editor.commit(); // Commit the edit, i.e., save the state of the flag!
}
}
}
答案 0 :(得分:0)
我刚注意到你的checkboxVideoType
是这个类的私有属性,我在任何地方都看不到setter。如果它为null,您将得到一个NullPointerException,它将导致FC。您需要传递对复选框对象的引用(可能作为versionCheckbox()
方法的参数)。