我是Android编程的新手,我试图在我的手电筒应用程序上应用主题。当我尝试应用两个主题中的一个时,我点击“确定”按钮时出现错误。
这是我在SimpleNotificationAppActivity.java上的代码
public class SimpleNotificationAppActivity extends Activity implements android.content.DialogInterface.OnClickListener{
private boolean isFlashOn = false;
private Camera camera;
//private ImageButton button;
private Button button;
private MediaPlayer button2;
////////////
public final static int CREATE_DIALOG = -1;
public final static int THEME_HOLO_LIGHT = 0;
public final static int THEME_HOLO = 1;
int position;
///////////////
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setTheme(android.R.style.Theme_Holo_Light);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonFlashlight);
button2 = MediaPlayer.create(SimpleNotificationAppActivity.this,R.raw.two_tone_nav); //SOM DO CLIQUE
//button = (ImageButton) findViewById(R.drawable.image_on);
//button.setBackgroundResource(R.drawable.switch_on);
Context context = this;
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera.");
Toast.makeText(getApplicationContext(),
"Sorry, your device doesn't have camera :(",
Toast.LENGTH_SHORT).show();
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isFlashOn) {
Log.i("info", "torch is turned off!");
button2.start();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
isFlashOn = false;
//button.setText("Torch-ON");
button.setBackgroundResource(R.drawable.button_on);
} else {
Log.i("info", "torch is turned on!");
button2.start();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
//button.setText("Torch-OFF");
button.setBackgroundResource(R.drawable.button_off);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.theme:
themeMenuItem();
break;
case R.id.about:
aboutMenuItem();
break;
}
return true;
}
private void aboutMenuItem(){
new AlertDialog.Builder(this)
.setTitle("About")
.setMessage("Flashlight app developed and designed by Rui Moreira.\n\nfacebook.com/RuiSousaMoreira")
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setIcon(R.drawable.app_icon_small)
.show();
}
private void themeMenuItem(){
//setTheme(android.R.style.Theme_Holo);
//setTheme(R.layout.main2);
////////////////////////////////////////////////////
position = getIntent().getIntExtra("position", -1);
switch(position)
{
case CREATE_DIALOG:
createDialog();
break;
case THEME_HOLO_LIGHT:
setTheme(android.R.style.Theme_Holo_Light);
break;
case THEME_HOLO:
setTheme(android.R.style.Theme_Holo_Light_DarkActionBar);
break;
default:
}
//super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
////////////////////////////////////////////////////
}
private void createDialog()
{
/** Options for user to select*/
String choose[] = {"Holo Light","Holo Dark"};
AlertDialog.Builder b = new AlertDialog.Builder(this);
/** Setting a title for the window */
b.setTitle("Choose your Application Theme");
/** Setting items to the alert dialog */
b.setSingleChoiceItems(choose, 0, null);
/** Setting a positive button and its listener */
b.setPositiveButton("OK", this);//
/*TRIED THIS TOO, BUT DOESNT SOLVES THE PROBLEM
*
* b.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
///TESTE
AlertDialog alert = (AlertDialog)dialog;
int position = alert.getListView().getCheckedItemPosition();
finish();
Intent intent = new Intent();
intent.putExtra("position", position);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
///TESTE
}
});*/
/** Setting a positive button and its listener */
b.setNegativeButton("Cancel", null);
/** Creating the alert dialog window using the builder class */
AlertDialog d = b.create();
/** show dialog*/
d.show();
}
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
AlertDialog alert = (AlertDialog)dialog;
int position = alert.getListView().getCheckedItemPosition();
finish();
Intent intent = new Intent(this, SimpleNotificationAppActivity.class);
intent.putExtra("position", position);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
基本上我点击这里的“主题”:
此对话框显示:
我选择主题,然后按“确定”更新应用主题。 谁能帮助我实现这一目标?我赞成任何建议:)
答案 0 :(得分:0)
正如您所尝试的那样,setTheme()
是改变主题的方法。但是,
请注意,应在实例化任何视图之前调用此方法 Context(例如在调用setContentView(View)或之前) inflate(int,ViewGroup))。
因此,您必须重新启动应用。我的,可能是肮脏的解决方案是杀死Activity
并使用Intent
中有关必须应用的主题的一些信息来调用您应用的主要活动。
答案 1 :(得分:0)
正如Little Child所说,仅仅使用setTheme()
是行不通的。它应该在setContentView(View)
之前使用。您需要做的是在按下“确定”按钮后重新启动应用程序,然后在应用程序重新启动时通过setTheme()设置新主题(在setContentView(View)之前执行此操作)。
要在一秒钟后重新启动应用程序,您可以使用它(根据您的应用程序替换活动):
Intent intent=new Intent (this, MainActivity.class);
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + 1000,
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT
| PendingIntent.FLAG_CANCEL_CURRENT));
finish();
以下是我在评论中所说的示例代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
String theme=shared.getString("theme", null);
if(theme != null && theme.equals("LIGHT")){
setTheme(R.style.Theme_light);
}else{
setTheme(R.style.Theme_dark);
}
setContentView(R.layout.activity_main);
}