0

I'm getting a resource not found error when I try to do this:

    TabWidget tabz = FindViewById<TabWidget>(Resource.Id.tabs);

compiler doesn't see the TabWidget even when it's clearly labeled by id in my Main.axml file

Error CS0117 'Resource.Id' does not contain a definition for 'tabs'

Here's my full code:

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="visible" />
<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp" />
</LinearLayout>

EDIT, sorry, this is the full code... I don't know why I didn't copy this last time. The above example is missing TabHost

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minWidth="25px"
        android:minHeight="25px"
        android:id="@+id/tabHost1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/linearLayout1">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </TabHost>

</LinearLayout>

How can I force the compiler to recognize my tabs id? I've tried rebuilding the resource.designer.cs .. putting the declaration of TabWidget inside all of the activities... nothing works. My C# can't see the TabWidget

full project can be found here:

https://github.com/hexag0d/BitChute_Mobile_Android_a2

I also tried this and it didn't work Xamarin Android Resource file not found

thanks, in advance

hexagod
  • 449
  • 3
  • 15
  • the problem is with the ResourceDesigner.cs file just comment this `TabWidget tabz = FindViewById(Resource.Id.tabs);` and Rebuild your solution and it should start working properly if not revert – FreakyAli Oct 30 '18 at 11:21
  • @G.hakim lol if I comment that out, then my TabWidget won't be accessible... – hexagod Oct 30 '18 at 22:44
  • @hexagodI don't think you understood what I was saying just check my answer out below – FreakyAli Oct 31 '18 at 06:27

2 Answers2

0

About the questions in your comment:

1,

that is very good advice, but one note is that when SetContentView(localLayout); is used inside an activity that already has a content view, it crashes the app.

The app crashed because localLayout in SetContentView(localLayout) is a LinearLayout but a TabWidget needs a TabHost. So you can directly use:

SetContentView(Resource.Layout.Main);

where Main.xaml is like:

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:theme="@style/MyTheme">

   <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.0"
        android:visibility="visible" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" />
    </LinearLayout>
</TabHost>

2,

LinearLayout localLayout = FindViewById(Resource.Layout.Main); View whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.Main, null); TextView tv = FindViewById(Resource.Id.textView169); TabHost _tabHost = (TabHost)whichYouWantToUse_findViewById.FindViewById(Resource.Id.tabhost); ' still can't see the tabhost ... I'm losing my mind here

The reason why you cannot see the tabhost is, firt, TabHost is FrameLayout, not LinearLayout, second, Resource.Id.tabhost should be Android.Resource.Id.TabHost. So I edited your code to :

    FrameLayout localLayout = FindViewById<FrameLayout>(Resource.Layout.Main);
    whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.Main, null);
    //TextView tv = FindViewById<TextView>(Resource.Id.textView1);
    TabHost _tabHost = (TabHost)whichYouWantToUse_findViewById.FindViewById(Android.Resource.Id.TabHost);

3,

So the interesting thing is if I change tabhost to tahost ala android:id="@+id/tahost" the resource appears in the designer but when I change to android:id="@+id/tabhost" the resource disappears.. very strange..

The id of TabHost need always be @android:id/tabhost", it cannot be modified to tahost or any others.

So the code in the onCreat Method is like this:

    base.OnCreate(bundle);

    SetContentView(Resource.Layout.Main);
    View whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.layout1, null);
    TextView textInAnotherLayout = (TextView)whichYouWantToUse_findViewById.FindViewById(Resource.Id.textView1);
    textInAnotherLayout.Text = "Yeah";

    TabHost tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
    TabWidget tabWidget = tabHost.TabWidget;
    var tabContent = tabHost.TabContentView;

    FrameLayout localLayout = FindViewById<FrameLayout>(Resource.Layout.Main);
    whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.Main, null);
    TabHost _tabHost = (TabHost)whichYouWantToUse_findViewById.FindViewById(Android.Resource.Id.TabHost);

The following is the old answer.


Answer modified: The way to get TabHost and TabWidget in Activity:

TabHost tabHost = this.TabHost;
TabWidget tabWidget = tabHost.TabWidget;

The way to use one resource from one XAML file in the activity for another:

    LinearLayout localLayout = FindViewById<LinearLayout>(Resource.Layout.Main);
    SetContentView(localLayout);
    View whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.layout1, null);
    Button button1 = (Button)whichYouWantToUse_findViewById.FindViewById(Resource.Id.button1);

I download your project and fixed this problem by modifying the property of >android:id

AbbyWang - MSFT
  • 650
  • 4
  • 6
  • that's wassup!! I'll test it out this evening when I get home. thanks playboy – hexagod Oct 30 '18 at 20:17
  • how do i make this work with TabHost ? com.android.layoutlib.bridge.impl.RenderSessionImpl$PostInflateException: TabHost requires a TabWidget with id "android:id/tabs" – hexagod Oct 31 '18 at 15:18
  • After doing the research for your problem, I found that TabWidget is a special Layout in android, you cannot change the id and you have to get the widget by (tabHost.TabWidget) in your C# code. Now I will modify my answer and sorry for the wrong answer before. – AbbyWang - MSFT Nov 01 '18 at 02:51
  • no worries... thank you man. We're all learning here. Appreciate the help my dude – hexagod Nov 01 '18 at 14:09
  • . now I'm just trying to figure out how use one resource from one XAML file in the activity for another. I think I need to use a view inflation ? Not expecting you to do my work tho... I'll post the answer once I know – hexagod Nov 01 '18 at 20:57
  • I have modified the answer and also written an example code to use one resource from one XAML file in the activity for another. – AbbyWang - MSFT Nov 02 '18 at 03:33
  • my code still can't see the "tabs" resource or the "tabhost" inside an activity... Resource.Id does not contain a definition for tabs Resource.Id does not contain a definition for tabhost when I put inside the Activity but I do have resource definitions when I use this inside the MainActivity ... so I can go MainActivity >> Activity resource but not Activity >> MainActivity tabhost or tabs ... strange, Still working on it. – hexagod Nov 02 '18 at 16:38
  • that is very good advice, but one note is that when SetContentView(localLayout); is used inside an activity that already has a content view, it crashes the app. – hexagod Nov 02 '18 at 17:23
  • ' LinearLayout localLayout = FindViewById(Resource.Layout.Main); View whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.Main, null); TextView tv = FindViewById(Resource.Id.textView169); TabHost _tabHost = (TabHost)whichYouWantToUse_findViewById.FindViewById(Resource.Id.tabhost); ' still can't see the tabhost ... I'm losing my mind here – hexagod Nov 02 '18 at 17:28
  • So the interesting thing is if I change tabhost to tahost ala android:id="@+id/tahost" the resource appears in the designer but when I change to android:id="@+id/tabhost" the resource disappears.. very strange.. I'm going to try manually adding it into resource.designer.cs – hexagod Nov 02 '18 at 19:34
  • I set the property using simply: TabHost tabHost; tabHost.TabWidget.Visibility = Android.Views.ViewStates.Gone; inside OnClick click listener https://github.com/hexag0d/BitChute_Mobile_Android_a2/blob/2.762/Activities/ClickListeners.cs and it worked... but still no object reference for tabhost when using the clever method you provided me inside my Activity classes perhaps this is because the tabhost properties are only accessible within the tabhost itself? I'm still working on this... very much appreciate the advice you've given me on inflating views. – hexagod Nov 03 '18 at 01:58
  • I modified my answer yesterday according to your questions in your commen, but fogot to add this comment. Please refer to the modified answer and I think it would solve your problems and confusions. – AbbyWang - MSFT Nov 06 '18 at 01:28
0

The issue is not with the visual studio compiler but the way that you have declared the id for your View,

If you check this blog by James White you will see how Id and the Layout Attributes actually work in Android,

The Right way of declaring the Id would be something like this android:id="@+id/tabs" whereas what you are doing is this android:id="@android:id/tabs"

Hence, When you do add them and try to find it using Resource.Id.tabs, There is nothing available in Id by the name tabs as it was never added by the Xamarin ResourceDesginer.cs file.

So the end result should actually be something like this :

  <TabHost
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffc916"
                android:orientation="vertical">
        </LinearLayout>
        </FrameLayout>
    </LinearLayout>
  </TabHost>

Also, note that I have replaced all the fill_parent's with match_parent's the reason behind it being that fill parent is now obsolete or you can say replaced (After API-8) and hence must not be used, which can be found here.

Update:

I have updated the way tab host works, Also updated the XML above and I am adding a reference link where you can find the right way of using both the TabHost and TabWidget, Also a good SO example for the same.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • mono.android.DesignerException: com.android.layoutlib.bridge.impl.RenderSessionImpl$PostInflateException at mono.android.DesignerException.fromThrowable(DesignerException.java:54) at mono.android.DesignerSession.checkRenderResultForError(DesignerSession.java:235) at mono.android.DesignerSession.load(DesignerSession.java:314) at mono.android.MessageListener$Runner.run(MessageListener.java:44) at java.lang.Thread.run(Thread.java:748) Caused by: com.android.layoutlib.bridge.impl.RenderSessionImpl$PostInflateException: TabHost requires a TabWidget with id "android:id/tabs". – hexagod Oct 31 '18 at 14:51
  • .. for some reason when I copied my code it was missing the TabHost. I've updated it to reflect my TabHost ... and I'm getting the above exception – hexagod Oct 31 '18 at 15:13
  • thanks for the help guys... the real problem is that I'm unable to use resources from different XAML files without using what I'm pretty sure is supposed to be a view inflator. I'm trying to work out the details now. I'll post the full answer once I figure it out. – hexagod Nov 01 '18 at 20:31
  • FYI, tabhost has to look like this or the app crashes – hexagod Nov 01 '18 at 21:26
  • I had already done that do take a look and also i have updated the code – FreakyAli Nov 02 '18 at 06:05
  • No the tabhost literally has to be named tabhost and not tabHost or this exception will occur Java.Lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost' – hexagod Nov 02 '18 at 17:44
  • Okay whatever i wouldn't be using this anyway, its just gotten old now you use viewpagers anyway. And okay updating it to tabhost – FreakyAli Nov 02 '18 at 19:00
  • yeah lol .. I don't blame you. Thanks for trying. I don't even think this is possible to do. I'm so far into the project though and the tabhost works fine... performance is better than the viewpager. Someone else made a version of my app with the viewpager and the performance isn't as good as the actionbar.tabhost ... I appreciate the help anyway G.hakim – hexagod Nov 02 '18 at 20:31
  • Okay anyway i will look into this thing later maybe anyway thanks for your time as well got to learn something new for a change \ – FreakyAli Nov 05 '18 at 05:08
  • uhh no... thank you sir. You're the one helping me with my issue. =] .. I'm still working on this. It's a tricky little bugger. I think we can only reference the tabhost from inside the tabhost. Any attempt to set a parameter of the tabhost from in an activity is failing. I did get the reference to work when the action modifies a property of tabhost ... so for example onclick of tab hides the tabhost – hexagod Nov 07 '18 at 18:00