1

我遇到了 onCreateContextMenu 仅在 API 15 和 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)

还有什么我可以提供的吗?

如果我可以提供任何其他信息来帮助解决此问题,请告诉我。

谢谢大家!


附加代码(根据要求)

列表活动

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

2 回答 2

0

Application我最好的猜测是,您以某种方式使用 an而不是 an创建小部件Activity,例如盲目地使用getApplicationContext()某个地方。当 Android 显示上下文菜单时,它会尝试获取与小部件关联的 Activity,但会遇到一个Application相反的情况,然后崩溃。

于 2013-02-14T12:33:53.140 回答
0

我解决了这个问题。

问题是因为我在不知道的情况下覆盖了现有的 API 方法getApplicationContext() 。

引起我所有头痛的方法是这样的:

public Context getApplicationContext(){
    return applicationContext;
}

我在寻找将上下文传递给我的方法的更好方法时发现了这段代码。事实证明,在您完全理解之前,您不应该使用任何代码!

希望其他人将来会发现此答案有用!

于 2013-02-15T09:40:15.797 回答