gpt4 book ai didi

android - onCreateContextMenu 仅在 API 15 和 16 上抛出错误

转载 作者:行者123 更新时间:2023-11-30 03:46:47 27 4
gpt4 key购买 nike

我遇到 onCreateContextMenu 问题,仅在 API 15 和 16 上抛出错误。它在从 API 8 到 API 17(我的最小和最大目标)的所有其他 API 上工作得很好。

我尽了最大努力尝试通过 Google 搜索自行解决这个问题,但大约一周后,我变得绝望到向出色的堆栈溢出社区请求帮助。

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)

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

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

提前谢谢大家!


附加代码(根据要求)

列表 Activity

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

最佳答案

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

关于android - onCreateContextMenu 仅在 API 15 和 16 上抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14873309/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com