提供使用旋转器更改主题的选项



我对java和android编程非常陌生,因此,我的理解不是很扎实,因此,如果我在这里提供一些帮助,我会非常感激。

我想提供一个选项,允许最终用户使用旋转器更改主题。然而,当我运行应用程序时,它在启动时失败了。应用程序的主活动如下:

public class ReminderListActivity extends ListActivity implements OnItemClickListener
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this); //For applying a theme
    setContentView(R.layout.reminder_list);

    Spinner spin = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, 
            themes);
    spin.setAdapter(aa);    
    spin.OnItemClickListener(this); //-->unable to call onItemClick  method. 

}
@Override
public void OnItemClick(AdapterView<?> parent, View view, int position, long arg3) {
    // TODO Auto-generated method stub

    switch(view.getId())
    {
    case 0:
        Utils.changeToTheme(this, Utils.THEME_DEFAULT);
        break;
    case 1:
        Utils.changeToTheme(this, Utils.THEME_WHITE);
        break;
    case 2:
        Utils.changeToTheme(this, Utils.THEME_BLUE);
        break;
    default:
        Utils.changeToTheme(this, Utils.THEME_WHITE);
        break;
    }
}           

Utils类如下:

private static int sTheme;
public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;
/**
 * Set the theme of the Activity, and restart it by creating a new Activity
 * of the same type.
 */
public static void changeToTheme(Activity activity, int theme)
{
    sTheme = theme;
    activity.finish();
    activity.startActivity(new Intent(activity, activity.getClass()));
}


/** Set the theme of the activity, according to the configuration. */
public static void onActivityCreateSetTheme(Activity activity)
{
    switch (sTheme)
    {
    default:
    case THEME_DEFAULT:
        break;
    case THEME_WHITE:
        activity.setTheme(R.style.Theme_White);
        break;
    case THEME_BLUE:
        activity.setTheme(R.style.Theme_Blue);
        break;
    }
}

添加到初始问题:

我已经将onItemClickListener更改为OnItemSelectedListener。现在应用程序不会崩溃,但当它加载时,屏幕一直闪烁。

代码如下:

public class ReminderListActivity extends ListActivity implements OnItemSelectedListener
{
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private RemindersDbAdapter mDbHelper;
String[] themes = { "Default", "White", "Blue"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this); //For applying a theme
    setContentView(R.layout.reminder_list);
    mDbHelper = new RemindersDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
    Spinner spin = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, 
            themes);
    spin.setAdapter(aa);    
    //spin.setOnItemSelectedListener(this);//-->problem here unable to call onItemClick method. 
                                      //What is image adapter
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

    switch(arg1.getId())
    {
    case 0:
        Utils.changeToTheme(this, Utils.THEME_DEFAULT);
        break;
    case 1:
        Utils.changeToTheme(this, Utils.THEME_WHITE);
        break;
    case 2:
        Utils.changeToTheme(this, Utils.THEME_BLUE);
        break;
    default:
        Utils.changeToTheme(this, Utils.THEME_WHITE);
        break;
    }
}           

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    Utils.changeToTheme(this, Utils.THEME_DEFAULT);
}

这是我的LogCat

05-10 09:12:56.449: E/Trace(1004): error opening trace file: No such file or directory  (2)
05-10 09:12:59.369: D/dalvikvm(1004): GC_CONCURRENT freed 180K, 11% free 2675K/2984K,  paused 86ms+10ms, total 242ms
05-10 09:12:59.369: D/dalvikvm(1004): WAIT_FOR_CONCURRENT_GC blocked 61ms
05-10 09:12:59.403: I/dalvikvm-heap(1004): Grow heap (frag case) to 3.342MB for 635812-byte allocation
05-10 09:12:59.529: D/dalvikvm(1004): GC_FOR_ALLOC freed 59K, 11% free 3236K/3608K, paused 120ms, total 121ms
05-10 09:12:59.609: D/dalvikvm(1004): GC_FOR_ALLOC freed <1K, 11% free 3236K/3608K, paused 57ms, total 58ms
05-10 09:12:59.622: I/dalvikvm-heap(1004): Grow heap (frag case) to 3.760MB for 500416-byte allocation
05-10 09:12:59.779: D/dalvikvm(1004): GC_FOR_ALLOC freed <1K, 10% free 3724K/4100K, paused 152ms, total 152ms
05-10 09:13:00.039: D/dalvikvm(1004): GC_CONCURRENT freed 1K, 10% free 3728K/4100K, paused 33ms+96ms, total 261ms
05-10 09:13:00.799: I/Choreographer(1004): Skipped 33 frames!  The application may be doing too much work on its main thread.
05-10 09:13:00.889: D/gralloc_goldfish(1004): Emulator without GPU emulation detected.
05-10 09:13:01.549: I/Choreographer(1004): Skipped 39 frames!  The application may be doing too much work on its main thread.
05-10 09:13:03.999: D/dalvikvm(1004): GC_CONCURRENT freed 770K, 22% free 3405K/4356K, paused 97ms+30ms, total 339ms
05-10 09:13:03.999: D/dalvikvm(1004): WAIT_FOR_CONCURRENT_GC blocked 24ms
05-10 09:13:04.689: I/Choreographer(1004): Skipped 152 frames!  The application may be doing too much work on its main thread.
05-10 09:13:05.899: I/Choreographer(1004): Skipped 68 frames!  The application may be doing too much work on its main thread.
05-10 09:13:07.825: I/Choreographer(1004): Skipped 66 frames!  The application may be doing too much work on its main thread.
05-10 09:13:10.030: I/Choreographer(1004): Skipped 70 frames!  The application may be doing too much work on its main thread.
05-10 09:13:10.471: I/Choreographer(1004): Skipped 38 frames!  The application may be doing too much work on its main thread.
05-10 09:13:11.510: I/Choreographer(1004): Skipped 32 frames!  The application may be doing too much work on its main thread.
05-10 09:13:13.550: I/Choreographer(1004): Skipped 74 frames!  The application may be doing too much work on its main thread.
05-10 09:13:14.040: I/Choreographer(1004): Skipped 37 frames!  The application may be doing too much work on its main thread.
05-10 09:13:16.000: I/Choreographer(1004): Skipped 47 frames!  The application may be doing too much work on its main thread.
05-10 09:13:16.530: I/Choreographer(1004): Skipped 50 frames!  The application may be doing too much work on its main thread.
05-10 09:13:18.570: I/Choreographer(1004): Skipped 128 frames!  The application may be doing too much work on its main thread.
05-10 09:13:20.809: D/dalvikvm(1004): GC_CONCURRENT freed 151K, 16% free 3668K/4344K, paused 80ms+36ms, total 434ms
05-10 09:13:21.170: I/Choreographer(1004): Skipped 99 frames!  The application may be doing too much work on its main thread.
05-10 09:13:22.959: I/Choreographer(1004): Skipped 82 frames!  The application may be doing too much work on its main thread.
05-10 09:13:24.149: I/Choreographer(1004): Skipped 50 frames!  The application may be doing too much work on its main thread.
05-10 09:13:24.679: I/Choreographer(1004): Skipped 48 frames!  The application may be doing too much work on its main thread.
05-10 09:13:27.719: I/Choreographer(1004): Skipped 75 frames!  The application may be doing too much work on its main thread.
05-10 09:13:29.279: I/Choreographer(1004): Skipped 45 frames!  The application may be doing too much work on its main thread.
05-10 09:13:31.061: I/Choreographer(1004): Skipped 84 frames!  The application may be doing too much work on its main thread.
05-10 09:13:31.449: I/Choreographer(1004): Skipped 31 frames!  The application may be doing too much work on its main thread.
05-10 09:13:33.880: I/Choreographer(1004): Skipped 37 frames!  The application may be doing too much work on its main thread.
05-10 09:13:34.249: I/Choreographer(1004): Skipped 30 frames!  The application may be doing too much work on its main thread.
05-10 09:13:36.230: D/dalvikvm(1004): GC_CONCURRENT freed 114K, 9% free 3939K/4308K, paused 47ms+69ms, total 832ms
05-10 09:13:37.539: I/Choreographer(1004): Skipped 75 frames!  The application may be doing too much work on its main thread.
05-10 09:13:39.810: I/Choreographer(1004): Skipped 53 frames!  The application may be doing too much work on its main thread.
05-10 09:13:40.970: I/Choreographer(1004): Skipped 41 frames!  The application may be doing too much work on its main thread.
05-10 09:13:43.230: I/Choreographer(1004): Skipped 51 frames!  The application may be doing too much work on its main thread.
05-10 09:13:43.672: I/Choreographer(1004): Skipped 39 frames!  The application may be doing too much work on its main thread.
05-10 09:13:45.672: I/Choreographer(1004): Skipped 106 frames!  The application may be doing too much work on its main thread.
05-10 09:13:47.770: I/Choreographer(1004): Skipped 87 frames!  The application may be doing too much work on its main thread.
05-10 09:13:50.149: D/dalvikvm(1004): GC_CONCURRENT freed 116K, 6% free 4226K/4472K, paused 79ms+50ms, total 544ms

关于将监听器从OnItemClickListener更改为onselecteditemlistener。应用程序没有崩溃,但似乎跳帧。但是,如果没有调用onItemSelectedListener方法,则应用程序将正常运行。

App加载THEME_WHITE

添加到现有问题:

整个onCreate()方法如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this); //For applying a theme
    setContentView(R.layout.reminder_list);
    mDbHelper = new RemindersDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
    Spinner spin = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, 
            themes);
    spin.setAdapter(aa);    
    spin.setOnItemSelectedListener(this);
} 

Spinner使用onItemSelected而不是onItemClick()。把

spin.setOnItemClickListener(this)

spin.setOnItemSelectedListener(this);

然后也改变

public class ReminderListActivity extends ListActivity implements OnItemClickListener

public class ReminderListActivity extends ListActivity implements OnItemSelectedListener

这就是为什么你的onItemClick()没有被调用,但我不认为它应该使你的程序崩溃。所以,它可能不能回答你当前的问题,但会回答你的下一个问题。

请张贴您的日志,以便我们确定您当前问题的根源。

相关内容

  • 没有找到相关文章

最新更新