无法将 contextImp 转换为 Activity 错误?
could not cast contextImp to Activity error?
在我的应用程序中,我有一个自定义 Activity
所有活动都从它扩展。我在那个mainActivity做了一些常用的操作。但是,我遇到了一个问题,我试图在扩展 ActionBarActivity
的主 class 中设置自定义操作栏。我收到标题错误。这是我遇到此错误的代码行:
public class OSActivity extends Activity
{
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.context = this.getBaseContext();
this.SetCustomActionBar(this.context);
}
private void SetCustomActionBar(final Context context)
{
((Activity)context).getActionBar().setDisplayShowHomeEnabled(false);
((Activity)context).getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(context);
View acionBar = inflater.inflate(R.layout.actionbar, null);
ImageView imgAppLogo = (ImageView)acionBar.findViewById(R.id.imgLogo);
TextView txtTitle = (TextView)acionBar.findViewById(R.id.txtTitle);
txtTitle.setText(context.getClass().getSimpleName());
ImageButton imgMenuButton = (ImageButton)acionBar.findViewById(R.id.imgMenuButton);
imgMenuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, v);
popup.getMenuInflater().inflate(R.menu.actionbar_menu_titles, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.debt_query) {
StartIntentFromActionBarMenuSelection(context, Activity1.class);
return true;
} else if (item.getItemId() == R.id.previous_payments) {
StartIntentFromActionBarMenuSelection(context, Activity2.class);
return true;
} else if (item.getItemId() == R.id.profile) {
StartIntentFromActionBarMenuSelection(context, Activity3.class);
return true;
} else if (item.getItemId() == R.id.about) {
StartIntentFromActionBarMenuSelection(context, Activity4.class);
return true;
}
return true;
}
});
popup.show();
}
});
((Activity)context).getActionBar().setCustomView(acionBar);
((Activity)context).getActionBar().setDisplayShowCustomEnabled(true);
}
private void StartIntentFromActionBarMenuSelection(Context context, Class<?> activity)
{
Intent intent = new Intent(context, activity);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
此外,这是我遇到的错误:
Caused by: java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
at com.some.some.SetCustomActionBar(OSActivity.java:35)
at com.some.some.OSActivity.onCreate(OSActivity.java:30)
很快,我处理了上下文,但我无法从上下文中获取 activity。我该如何解决这个问题?
尝试施法:
((YourActivity)getActivity()).getActionBar().setDisplayShowCustomEnabled(true);
编辑:
最后,从 ActionBarActivity
扩展 Activity 而不是 Activity
解决了问题。
这里的问题是因为使用了getBaseContext()
。
一方面我们有 Context
类型 ContextImpl
的实现,但另一方面我们还有 Context
,它们是用 ContextWrapper
实现的。
ContextWrapper
是一个 Adapter 模式,其基础 - ContextImpl
作为字段。
Activity 本质上是 ContextWrapper
或更具体地说 ContextThemeWrapper
为您的上下文行为添加主题。
您所做的是获取 activity 的基础上下文(ContextWrapper 的基础)并期望您能够再次将其转换为 activity。
在许多情况下,没有理由使用 baseContext 或 applicationContext,尤其是在 activity 中,您需要将上下文 Themed
用于视图层次结构更新。
所以不用
this.context = this.getBaseContext();
this.SetCustomActionBar(this.context);
你应该使用:
this.SetCustomActionBar(this);
在我的应用程序中,我有一个自定义 Activity
所有活动都从它扩展。我在那个mainActivity做了一些常用的操作。但是,我遇到了一个问题,我试图在扩展 ActionBarActivity
的主 class 中设置自定义操作栏。我收到标题错误。这是我遇到此错误的代码行:
public class OSActivity extends Activity
{
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.context = this.getBaseContext();
this.SetCustomActionBar(this.context);
}
private void SetCustomActionBar(final Context context)
{
((Activity)context).getActionBar().setDisplayShowHomeEnabled(false);
((Activity)context).getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(context);
View acionBar = inflater.inflate(R.layout.actionbar, null);
ImageView imgAppLogo = (ImageView)acionBar.findViewById(R.id.imgLogo);
TextView txtTitle = (TextView)acionBar.findViewById(R.id.txtTitle);
txtTitle.setText(context.getClass().getSimpleName());
ImageButton imgMenuButton = (ImageButton)acionBar.findViewById(R.id.imgMenuButton);
imgMenuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, v);
popup.getMenuInflater().inflate(R.menu.actionbar_menu_titles, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.debt_query) {
StartIntentFromActionBarMenuSelection(context, Activity1.class);
return true;
} else if (item.getItemId() == R.id.previous_payments) {
StartIntentFromActionBarMenuSelection(context, Activity2.class);
return true;
} else if (item.getItemId() == R.id.profile) {
StartIntentFromActionBarMenuSelection(context, Activity3.class);
return true;
} else if (item.getItemId() == R.id.about) {
StartIntentFromActionBarMenuSelection(context, Activity4.class);
return true;
}
return true;
}
});
popup.show();
}
});
((Activity)context).getActionBar().setCustomView(acionBar);
((Activity)context).getActionBar().setDisplayShowCustomEnabled(true);
}
private void StartIntentFromActionBarMenuSelection(Context context, Class<?> activity)
{
Intent intent = new Intent(context, activity);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
此外,这是我遇到的错误:
Caused by: java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity
at com.some.some.SetCustomActionBar(OSActivity.java:35)
at com.some.some.OSActivity.onCreate(OSActivity.java:30)
很快,我处理了上下文,但我无法从上下文中获取 activity。我该如何解决这个问题?
尝试施法:
((YourActivity)getActivity()).getActionBar().setDisplayShowCustomEnabled(true);
编辑:
最后,从 ActionBarActivity
扩展 Activity 而不是 Activity
解决了问题。
这里的问题是因为使用了getBaseContext()
。
一方面我们有 Context
类型 ContextImpl
的实现,但另一方面我们还有 Context
,它们是用 ContextWrapper
实现的。
ContextWrapper
是一个 Adapter 模式,其基础 - ContextImpl
作为字段。
Activity 本质上是 ContextWrapper
或更具体地说 ContextThemeWrapper
为您的上下文行为添加主题。
您所做的是获取 activity 的基础上下文(ContextWrapper 的基础)并期望您能够再次将其转换为 activity。
在许多情况下,没有理由使用 baseContext 或 applicationContext,尤其是在 activity 中,您需要将上下文 Themed
用于视图层次结构更新。
所以不用
this.context = this.getBaseContext();
this.SetCustomActionBar(this.context);
你应该使用:
this.SetCustomActionBar(this);