我遇到的问题是onCreateContextMenu仅在API 15& amp; 16.它在从API 8到API 17(我的最小和最大目标)的每个其他API上都能正常工作。
我尽力通过搜索谷歌来尝试自己解决这个问题,但大约一个星期后,我已经变得非常绝望,可以从精彩的堆栈溢出社区中寻求帮助。
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)
我能提供的其他任何内容?
如果有任何其他信息可以帮我解决这个问题,请告诉我。
提前谢谢大家!
ListActivity
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());
}
ArrayAdapter
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;
}
}
答案 0 :(得分:1)
我解决了这个问题。
问题是因为我在没有意识到的情况下覆盖了现有的API方法 getApplicationContext()。
导致我所有头痛的问题是:
public Context getApplicationContext(){
return applicationContext;
}
我在找到更好的方法将上下文传递给我的方法时发现了这段代码。事实证明,在你完全理解之前,你不应该使用任何代码!
希望其他人在将来发现这个答案有用!
答案 1 :(得分:0)
我最好的猜测是,您使用Application
而不是Activity
以某种方式创建窗口小部件,例如在某个地方盲目地使用getApplicationContext()
。当Android开始显示上下文菜单时,它会尝试获取与窗口小部件关联的活动,而不是运行Application
,然后崩溃。