0

首先,我想问一些事情,想知道我在做什么是对还是错。我正在为汽车媒体开发一个接口,它应该超过 android 的股票。

它没有官方按钮,所以我创建了一个 menu_layout,它由 2 个边框组成,每个边框都在一侧,带有图像按钮,您可以从中访问导航器、电话、调谐器等...

在这个布局中,有一个 FrameLayout,我将在其中添加与我需要的布局相对应的布局(媒体、导航器、电话......)

因此,主要布局是一个 FragmentActivity,我将其他“活动”设置为片段。

但是这样做的结果是,我在某些框架中失去了一些功能,所以......

我可以将此片段定义为 FragmentAcitivities 吗?或者它们必须是 Fragment 才能在容器上动态设置?

我问这个,因为一个片段是PhoneView。此片段包含用作拨号器的自定义 Imagebuttons 的定义。此外,此框架使用 editext、imagebuttons 等来扩展布局……所有这些都是定义自定义拨号器所必需的。

但是在执行时,我得到了这个:

java.lang.ClassCastException: android.widget.Imagebutton在这一行:

button1 = (ImageButton) getActivity().findViewById(R.id.one); 在定义拨号器的第一个按钮时。

那么,这是因为片段不能用这种资源膨胀布局吗?

更新——添加代码

phone_view.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 
android:baselineAligned="false">

    <!-- Layout for the dialer -->
<LinearLayout 
    android:id="@+id/Right_layout"
    android:layout_width="fill_parent"        
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_marginStart="@dimen/dialpad_horizontal_margin"
    android:background="#000000" > 

    <!-- Text field above the keypad where the digits are displayed -->
    <LinearLayout
        android:id="@+id/digits_container"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="@integer/dialpad_layout_weight_digits"
        android:layout_marginTop="@dimen/dialpad_vertical_margin"
        android:gravity="center"
        android:background="@drawable/dialpad_background" >

        <EditText
            android:id="@+id/digits"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:scrollHorizontally="true"
            android:textAppearance="@style/DialtactsDigitsTextAppearance"
            android:focusableInTouchMode="false"
            android:nextFocusRight="@+id/deleteButton"
            android:background="@android:color/transparent" />

        <ImageButton
            android:id="@+id/deleteButton"
            android:layout_width="56dip"
            android:layout_height="50dip"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:state_enabled="false"
            android:contentDescription="@string/description_delete_button"
            android:src="@drawable/ic_dial_action_delete" />
    </LinearLayout>

    <!-- The dialpad itself -->
    <include layout="@layout/dialpad" />

    <View
       android:layout_width="match_parent"
       android:layout_height="@dimen/dialpad_vertical_margin"
       android:background="#66000000"/>

    <FrameLayout
        android:id="@+id/dialButtonContainer"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="@integer/dialpad_layout_weight_additional_buttons"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/dialpad_background">

        <ImageButton
            android:id="@+id/dialButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:state_enabled="false"
            android:background="@drawable/btn_call"
            android:contentDescription="@string/description_dial_button"
            android:src="@drawable/ic_dial_action_call" />

</FrameLayout>        
</LinearLayout>

拨号盘.xml

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialpad"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="@integer/dialpad_layout_weight_dialpad"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/dialpad_vertical_margin"
android:paddingStart="5dip"
android:paddingEnd="5dip"
android:paddingBottom="10dip"
android:background="@drawable/dialpad_background"
android:layoutDirection="ltr">

<TableRow
     android:layout_height="0px"
     android:layout_weight="1" >
    <ImageButton 
        android:id="@+id/one" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_1_no_vm_wht"
        android:contentDescription="@string/description_image_button_one" />
    <ImageButton 
        android:id="@+id/two" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_2_wht"
        android:contentDescription="@string/description_image_button_two" />
    <ImageButton 
        android:id="@+id/three" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_3_wht"
        android:contentDescription="@string/description_image_button_three" />
</TableRow>

<TableRow
     android:layout_height="0px"
     android:layout_weight="1">
    <ImageButton 
        android:id="@+id/four" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_4_wht"
        android:contentDescription="@string/description_image_button_four" />
    <ImageButton 
        android:id="@+id/five" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_5_wht"
        android:contentDescription="@string/description_image_button_five" />
    <ImageButton 
        android:id="@+id/six" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_6_wht"
        android:contentDescription="@string/description_image_button_six" />
</TableRow>

<TableRow
     android:layout_height="0px"
     android:layout_weight="1">
    <ImageButton 
        android:id="@+id/seven" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_7_wht"
        android:contentDescription="@string/description_image_button_seven" />
    <ImageButton 
        android:id="@+id/eight" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_8_wht"
        android:contentDescription="@string/description_image_button_eight" />
    <ImageButton 
        android:id="@+id/nine" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_9_wht"
        android:contentDescription="@string/description_image_button_nine" />
</TableRow>

<TableRow
     android:layout_height="0px"
     android:layout_weight="1">
    <ImageButton 
        android:id="@+id/star" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_star_wht"
        android:contentDescription="@string/description_image_button_star" />
    <ImageButton 
        android:id="@+id/zero" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_0_no_plus_wht"
        android:contentDescription="@string/description_image_button_zero" />
    <ImageButton 
        android:id="@+id/pound" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_pound_wht"
        android:contentDescription="@string/description_image_button_pound" />
</TableRow>

PhoneView.java

public class PhoneView extends Fragment{
...

    ImageButton dialBtn;
    ImageButton clearBtn;
    EditText numTxt;
    ImageButton button1;
    ImageButton button2;
    ...

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mRoot = inflater.inflate(R.layout.phone_view, null);
    return mRoot;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
    ...

    button1 = (Imagebutton) getActivity().findViewById(R.id.one);
    ...

    /**Button1*/
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Set1(v);
        }
    });
}

public void Set1 (View view) {
    numTxt.setText(numTxt.getText()+"1");
}

更新 2——LogCat 上的新错误

将 .getActivity() 修改为 .getVew() 后,现在我在 LogCat 上得到了这个:

android.view.InflateException: Binary XML file line #20: Error inflating class

在这一行:mRoot = inflater.inflate(R.layout.phone_view, null); from the onCreateView()

几乎在最后告诉:Caused by: android.content.res.ResourcesNotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x101030e a=2}

at android.view.View.<init>
at android.widget.ImageView.<init>
at android.widget.ImageView.<init>
at android.widget.ImageView.<init>

不得不说,我已经在拨号盘中使用 Button 而不是 ImageButton 尝试了相同的代码,并且它可以工作。但我需要图像按钮。

更新 3——

可能是样式定义导致这种情况发生,所以这里看看可能出了什么问题:

<style name="DialpadButtonStyle">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_weight">1</item>
    <item name="android:background">?android:attr/selectableItemBackground</item>
    <item name="android:soundEffectsEnabled">false</item>
</style>
4

2 回答 2

3

正如我告诉你的,你正在寻找活动层次结构中的按钮,但它属于片段视图层次结构。改变

button1 = (Imagebutton) getActivity().findViewById(R.id.one);

button1 = (Imagebutton) getView().findViewById(R.id.one);

我仍然不确定你为什么得到 aClassCastException而不是 a NullPointerException。在再次运行之前,清理并重建您的项目

于 2013-11-08T12:09:37.013 回答
0

您需要在正在膨胀的片段上找到组件,而不是在主要活动上。搜索片段的布局

button1 =(Imagebutton) mRoot.findViewById(R.id.one);
于 2013-11-08T12:09:41.863 回答