API 15 と 16 でのみ onCreateContextMenu がエラーをスローするという問題があります。API 8 から API 17 (最小および最大ターゲット) までの他のすべての API で完全に正常に動作します。
Google で検索して、この問題を自分で解決しようと最善を尽くしましたが、約 1 週間後には、すばらしいスタック オーバーフロー コミュニティに助けを求めるほど必死になりました。
onCreateContextMenu メソッド
@Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
Log.i(TAG, "Create context menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu_my_time_zone, menu);
Log.i(TAG, "Context menu created.");
}
包含クラス
public class MyTimeZoneTActivity extends ListActivity {
// I can post the code for the Class if required.
}
例外
02-14 09:38:20.828: E/AndroidRuntime(10912): FATAL EXCEPTION: main
02-14 09:38:20.828: E/AndroidRuntime(10912): java.lang.ClassCastException: com.creativitality.labs.timezoneswidget.MyTimeZonesActivity cannot be cast to android.app.Application
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.view.Window.getCompatInfo(Window.java:481)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.view.Window$LocalWindowManager.<init>(Window.java:491)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.view.Window.setWindowManager(Window.java:477)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.view.Window.setWindowManager(Window.java:460)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.app.Dialog.<init>(Dialog.java:163)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.app.AlertDialog.<init>(AlertDialog.java:114)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.app.AlertDialog$Builder.create(AlertDialog.java:913)
02-14 09:38:20.828: E/AndroidRuntime(10912): at com.android.internal.view.menu.MenuDialogHelper.show(MenuDialogHelper.java:79)
02-14 09:38:20.828: E/AndroidRuntime(10912): at com.android.internal.view.menu.ContextMenuBuilder.show(ContextMenuBuilder.java:88)
02-14 09:38:20.828: E/AndroidRuntime(10912): at com.android.internal.policy.impl.PhoneWindow$DecorView.showContextMenuForChild(PhoneWindow.java:2201)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.widget.AbsListView.performLongPress(AbsListView.java:2926)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2871)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.os.Handler.handleCallback(Handler.java:615)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.os.Looper.loop(Looper.java:137)
02-14 09:38:20.828: E/AndroidRuntime(10912): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-14 09:38:20.828: E/AndroidRuntime(10912): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 09:38:20.828: E/AndroidRuntime(10912): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 09:38:20.828: E/AndroidRuntime(10912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-14 09:38:20.828: E/AndroidRuntime(10912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-14 09:38:20.828: E/AndroidRuntime(10912): at dalvik.system.NativeStart.main(Native Method)
他に提供できるものはありますか?
この問題の解決に役立つ情報が他にあればお知らせください。
よろしくお願いします!
追加コード (要求に応じて)
リスト アクティビティ
public class MyTimeZonesActivity extends ListActivity {
// Define application context
private Context applicationContext;
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
List<TimeZoneData> timeZoneData = null;
//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;
TimeZoneAdaptor timezoneAdaptor = null;
public static final int INSTRUCTIONS_CODE = 0;
private static final String TAG = "MyTimeZonesActivity";
SharedPreferences sharedPref;
SharedPreferencesHelper sharedPreferencesHelper;
TimeZonesApplication timeZonesApplication;
public Context getApplicationContext(){
return applicationContext;
}
@Override
public void onCreate(Bundle savedInstanceState){
Log.i(TAG, "My Time Zone Activity - onCreate called.");
super.onCreate(savedInstanceState);
// Get Application Globals
timeZonesApplication = (TimeZonesApplication)getApplicationContext();
// Set application context
applicationContext = this;
// Populate time zone data
timeZoneData = new ArrayList<TimeZoneData>();
getStoredTimeZonesData(); // Populates timeZoneData object with stored time zone data
setContentView(R.layout.activity_my_time_zones);
timezoneAdaptor = new TimeZoneAdaptor(this, R.layout.timezone_view_mine, timeZoneData);
setListAdapter(timezoneAdaptor);
registerForContextMenu(getListView());
}
アレイアダプター
public class TimeZoneAdaptor extends ArrayAdapter<TimeZoneData> {
private String TAG = "TimeZoneAdaptor";
//private ArrayList<TimeZoneData> mOrigionalValues;
private List<TimeZoneData> mObjects;
public TimeZoneAdaptor(Context context, int textViewResourceId, List<TimeZoneData> objects) {
super(context, textViewResourceId, objects);
this.mObjects = objects;
//this.mOrigionalValues = new ArrayList<TimeZoneData>(objects);
}
@Override
public int getCount() {
return ((null != mObjects) ? mObjects.size() : 0);
}
@Override
public TimeZoneData getItem(int position) {
return ((null != mObjects) ? mObjects.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public void add(TimeZoneData timeZone)
{
Log.w(TAG, "Add time zone: " + timeZone.id);
mObjects.add(timeZone);
notifyDataSetChanged();
}
public void remove(int position){
Log.i(TAG, "Remove object from adapter at position: " + position);
mObjects.remove(position);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater vi = (LayoutInflater) MyTimeZonesActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.timezone_view_mine, null);
}
TimeZoneData data = mObjects.get(position);
if (null != data) {
TextView textCountry = (TextView) view.findViewById(R.id.timezone_country);
TextView textName = (TextView) view.findViewById(R.id.timezone_name);
TextView textTime = (TextView) view.findViewById(R.id.timezone_time);
textCountry.setText(data.country.toUpperCase(Locale.getDefault()));
textName.setText(data.city);
textTime.setText(data.time);
//view.setOnClickListener(new OnItemClickListener(position)); // NOT REQUIRED as we are setting in on activity level
}
return view;
}
}