【问题标题】:Providing Option to change Themes Using Spinners提供使用微调器更改主题的选项
【发布时间】:2013-05-09 23:19:38
【问题描述】:

我对 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 更改为 OnSelectedItemListeher。该应用程序不会崩溃,但似乎正在跳帧。但是,如果不调用 onItemSelectedListener 方法,则应用程序可以正常运行。

应用加载“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);

} 

【问题讨论】:

  • 查看我对您部分问题的回答。但是,您的 xml 中是否有 ListViewandroid:id="@android:id/list"
  • 是的,我有你提到的 ListView

标签: android switch-statement spinner onitemclicklistener onitemclick


【解决方案1】:

Spinner 使用 onItemSelected 而不是 onItemClick()。改变

spin.setOnItemClickListener(this)

spin.setOnItemSelectedListener(this);

然后也改变

public class ReminderListActivity extends ListActivity implements OnItemClickListener

public class ReminderListActivity extends ListActivity implements OnItemSelectedListener

这就是您的onItemClick() 没有被调用的原因,但我认为它不会使您的程序崩溃。因此,它可能无法回答您当前的问题,但会回答您的下一个问题。请发布您的 logcat,以便我们确定您当前问题的根源。

【讨论】:

  • 我已经编辑了代码并包含了我的 logcat。希望对复习有所帮助。提前致谢
  • 请不要更改您的 OP,因为现在它对其他人来说是混乱和无用的。您可以编辑和添加但不要更改它。另外,这就是你所有的onCreate() 吗?我没有看到任何会导致问题的明显错误
  • 很抱歉造成的混乱。最初我没有包括我的整个 onCreate() 方法,因为我想让问题尽可能简单。无论如何,我已经添加了上面的整个 onCreate() 方法。希望它为你澄清它
  • 没问题。 fillData() 是做什么的?如果这非常密集,那么您可能希望将其放在单独的 Thread 中。根据日志,您在UI 上做了太多工作。这就是为什么我怀疑onCreate()
  • filldata() 执行以下操作 1. 创建一个数组以指定我们要在列表中显示的字段 2. 以及我们要绑定这些字段的字段数组 3. 然后,创建一个简单的光标适配器并将其设置为显示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多