Note that there are some explanatory texts on larger screens.

plurals
  1. POInflate imagebutton on fragment
    primarykey
    data
    text
    <p>First of all, I want to ask something, to know if what I'm doing is correct or wrong. I'm developing an interfaze for a car media, wich is supposed to be over the android stock.</p> <p>It won't have fisical buttons, so I've created a menu_layout wich consists on 2 borders, each on one side, with imagebuttos from where you can access to navigator, phone, tuner, etc...</p> <p>Inside this layout, there is a FrameLayout where dinamically I'll add the layout corresponding to what I need (media, navigator, phone...)</p> <p>The main layout so, is a FragmentActivity, and I set the other "activities" as fragments. </p> <p>But is resulting that doing this, I'm loosing some functionallity in some framents, so...</p> <p>Could I define this fragments as FragmentAcitivities? Or they must be Fragment to be able to set the dinamically on the container?</p> <p>I ask this, because one fragment is the PhoneView. This fragment contais the definitions for custom Imagebuttons that act as the dialer. Also, this frament inflates a layout with the editext, imagebuttons, etc... all necesary to define a custom dialer.</p> <p>But when executing, I get this:</p> <p><code>java.lang.ClassCastException: android.widget.Imagebutton</code> at this line:</p> <p><code>button1 = (ImageButton) getActivity().findViewById(R.id.one);</code> when defining the first button of the dialer.</p> <p>So, is this because the fragment can't inflate a layout with this kind of resources??</p> <p><strong>UPDATE -- Code added</strong></p> <p><strong>phone_view.xml</strong></p> <pre><code>&lt;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"&gt; &lt;!-- Layout for the dialer --&gt; &lt;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" &gt; &lt;!-- Text field above the keypad where the digits are displayed --&gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;!-- The dialpad itself --&gt; &lt;include layout="@layout/dialpad" /&gt; &lt;View android:layout_width="match_parent" android:layout_height="@dimen/dialpad_vertical_margin" android:background="#66000000"/&gt; &lt;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"&gt; &lt;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" /&gt; &lt;/FrameLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p></p> <p><strong>Dialpad.xml</strong></p> <pre><code>&lt;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"&gt; &lt;TableRow android:layout_height="0px" android:layout_weight="1" &gt; &lt;ImageButton android:id="@+id/one" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_1_no_vm_wht" android:contentDescription="@string/description_image_button_one" /&gt; &lt;ImageButton android:id="@+id/two" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_2_wht" android:contentDescription="@string/description_image_button_two" /&gt; &lt;ImageButton android:id="@+id/three" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_3_wht" android:contentDescription="@string/description_image_button_three" /&gt; &lt;/TableRow&gt; &lt;TableRow android:layout_height="0px" android:layout_weight="1"&gt; &lt;ImageButton android:id="@+id/four" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_4_wht" android:contentDescription="@string/description_image_button_four" /&gt; &lt;ImageButton android:id="@+id/five" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_5_wht" android:contentDescription="@string/description_image_button_five" /&gt; &lt;ImageButton android:id="@+id/six" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_6_wht" android:contentDescription="@string/description_image_button_six" /&gt; &lt;/TableRow&gt; &lt;TableRow android:layout_height="0px" android:layout_weight="1"&gt; &lt;ImageButton android:id="@+id/seven" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_7_wht" android:contentDescription="@string/description_image_button_seven" /&gt; &lt;ImageButton android:id="@+id/eight" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_8_wht" android:contentDescription="@string/description_image_button_eight" /&gt; &lt;ImageButton android:id="@+id/nine" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_9_wht" android:contentDescription="@string/description_image_button_nine" /&gt; &lt;/TableRow&gt; &lt;TableRow android:layout_height="0px" android:layout_weight="1"&gt; &lt;ImageButton android:id="@+id/star" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_star_wht" android:contentDescription="@string/description_image_button_star" /&gt; &lt;ImageButton android:id="@+id/zero" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_0_no_plus_wht" android:contentDescription="@string/description_image_button_zero" /&gt; &lt;ImageButton android:id="@+id/pound" style="@style/DialpadButtonStyle" android:src="@drawable/dial_num_pound_wht" android:contentDescription="@string/description_image_button_pound" /&gt; &lt;/TableRow&gt; </code></pre> <p></p> <p><strong>PhoneView.java</strong></p> <pre><code>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"); } </code></pre> <p><strong>UPDATE 2 -- New error on LogCat</strong></p> <p>After modifying the .getActivity() to .getVew(), now I'm getting this on LogCat:</p> <p>android.view.InflateException: Binary XML file line #20: Error inflating class</p> <p>at this line: <code>mRoot = inflater.inflate(R.layout.phone_view, null); from the onCreateView()</code></p> <p>at almost at the end tells: <code>Caused by: android.content.res.ResourcesNotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x101030e a=2}</code></p> <pre><code>at android.view.View.&lt;init&gt; at android.widget.ImageView.&lt;init&gt; at android.widget.ImageView.&lt;init&gt; at android.widget.ImageView.&lt;init&gt; </code></pre> <p>Have to say that I've tried the same code using Button in the dialpad instead of ImageButton and it works. But I need Imagebuttons.</p> <p><strong>UPDATE 3 --</strong></p> <p>Probably is the style definition wich is making this happen, so here is it to see what can be wrong:</p> <pre><code>&lt;style name="DialpadButtonStyle"&gt; &lt;item name="android:layout_width"&gt;0dip&lt;/item&gt; &lt;item name="android:layout_height"&gt;match_parent&lt;/item&gt; &lt;item name="android:layout_weight"&gt;1&lt;/item&gt; &lt;item name="android:background"&gt;?android:attr/selectableItemBackground&lt;/item&gt; &lt;item name="android:soundEffectsEnabled"&gt;false&lt;/item&gt; &lt;/style&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload