0

I am very new to java and android programming and hence, my understanding is not very solid and hence, I would be much obliged if am provided with some assistance here.

I want to provide an option to allow the end user to change the themes using a spinner. However, when I run the app, it fails on start up. The mainactivity of the app is as follows:

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;
    }
}           

The Utils class is as follows:

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;
    }
}

Added To Initial Problem:

I have changed the onItemClickListener to OnItemSelectedListener. Now the application does not crash but when it loads, the screen keeps on blinking.

The Code is as follows:

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);

}

Here is my 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

On changing the Listener from OnItemClickListener to OnSelectedItemListeher. The App does not crash but seems to be skipping frames. However, if the onItemSelectedListener method is not called, the app functions properly.

The App loads "THEME_WHITE".

ADDED TO EXISTING QUESTION:

The whole onCreate() method is as shown below:

@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);

} 
4

1 回答 1

0

SpinneronItemSelected不使用onItemClick()。改变

spin.setOnItemClickListener(this)

spin.setOnItemSelectedListener(this);

然后也改变

public class ReminderListActivity extends ListActivity implements OnItemClickListener

public class ReminderListActivity extends ListActivity implements OnItemSelectedListener

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

于 2013-05-09T23:49:38.387 回答