首先,我想问一些事情,知道我所做的事情是正确还是错误。我正在为汽车媒体开发一个interfaze,这应该是关于机器人股票的。
它没有fisical按钮,所以我创建了一个menu_layout,它由2个边框组成,每个边框都有一个图像按钮,你可以从那里访问导航器,电话,调谐器等......
在这个布局中,有一个FrameLayout,我会根据需要添加相应的布局(媒体,导航器,手机......)
主要布局是一个FragmentActivity,我将其他“活动”设置为片段。
但结果就是这样做,我在一些片段中失去了一些功能,所以...
我可以将此片段定义为FragmentAcitivities吗?或者它们必须是Fragment才能在容器上设置dinamically?
我问这个,因为一个片段是PhoneView。此片段包含充当拨号器的自定义图像按钮的定义。此外,这个框架使用editext,图像按钮等扩展布局......所有这些都需要定义自定义拨号器。
但是在执行时,我明白了:
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>
Dialpad.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:二进制XML文件行#20:错误导致类
在这一行: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尝试了相同的代码,它可以工作。但我需要Imagebuttons。
更新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>
答案 0 :(得分:3)
正如我告诉你的那样,你正在寻找活动层次结构中的按钮,但它属于Fragment视图层次结构。变化
button1 = (Imagebutton) getActivity().findViewById(R.id.one);
与
button1 = (Imagebutton) getView().findViewById(R.id.one);
我仍然不确定您为何获得ClassCastException
而不是NullPointerException
。在再次运行之前,请清理并重建项目
答案 1 :(得分:0)
您需要找到哪些片段正在膨胀的组件,而不是主要活动。 搜索片段的布局
button1 =(Imagebutton) mRoot.findViewById(R.id.one);