LinearLayout中不能被强制转换为android.widget.checkbox [英] LinearLayout cannot be cast to android.widget.checkbox

查看:435
本文介绍了LinearLayout中不能被强制转换为android.widget.checkbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现在面临的问题是,我的应用程序的在发布与上述错误崩溃时,包括下面的语句。

 外部=(复选框)menu.findItem(R.id.location).getActionView();

使用此语句code块是

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
    // TODO自动生成方法存根
    新MenuInflater(本).inflate(R.menu.actions,菜单);
    Log.d(动作,膨胀);
    外部=(复选框)menu.findItem(R.id.location).getActionView(); <<<错误
    Log.d(动作,外部初始化);
    返回super.onCreateOptionsMenu(菜单);
}

外部声明为

同一类的数据成员

 私人的CheckBox外部= NULL;

这是夸大的菜单是:

 <?XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目
     机器人:ID =@ + ID /位置
     机器人:actionLayout =@布局/ action_location
     机器人:showAsAction =从不>< /项目>
<项目
    机器人:ID =@ + ID /保存
    机器人:标题=保存
    机器人:showAsAction =总是>< /项目>
<项目
    机器人:ID =@ + ID / saveBackground
    机器人:showAsAction =总是| withText
    机器人:标题=,在BG>< /项目>

如果该特定行注释掉,应用程序启动就好了。可能是什么问题?

[更新]这里的action_location.xml含量

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<复选框
    机器人:ID =@ + ID /外部
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:标题=@字符串/外部
    />
< / LinearLayout中>


解决方案

getActionView()返回该菜单项当前设置的动作视图。试试这个方法。

 查看= menu.findItem(R.id.location).getActionView();
复选框复选框= view.findViewById(R.id.external);

The problem that I am facing is that my app crashes at launch with the above error when the following statement is included.

  external= (CheckBox) menu.findItem(R.id.location).getActionView();

The code block with this statement is

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // TODO Auto-generated method stub
    new MenuInflater(this).inflate(R.menu.actions,menu);
    Log.d("Action","inflated");
    external= (CheckBox) menu.findItem(R.id.location).getActionView();   <<<error
    Log.d("Action","external initialized");
    return super.onCreateOptionsMenu(menu);
}

external is a data member of the same class declared as

private CheckBox external=null;

The menu that is inflated is :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item 
     android:id="@+id/location"
     android:actionLayout="@layout/action_location"
     android:showAsAction="never">

</item>
<item 
    android:id="@+id/save" 
    android:title="Save" 
    android:showAsAction="always">

</item>
<item 
    android:id="@+id/saveBackground" 
    android:showAsAction="always|withText"
    android:title="in BG">

</item>

If that particular line is commented out, the app launches just fine. What could be the issue ?

[UPDATE] Here's the action_location.xml content

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox 
    android:id="@+id/external"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:title="@string/external"
    />


</LinearLayout>

解决方案

getActionView() returns the currently set action view for this menu item. Try this way

View view =  menu.findItem(R.id.location).getActionView(); 
CheckBox checkBox = view.findViewById(R.id.external);

这篇关于LinearLayout中不能被强制转换为android.widget.checkbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆