私は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);
}