Error referencing an inner class View in layout/main.xml
For inner classes the syntax becomes:
<view class="com.grafightscratch.ochemmer.MoleculeTablet$MoleculeTabletView" />
The reason is that $ is an illegal character in XML tags.
Error inflating inner class view
You need to add public static
to the inner class.
public
because it needs to be visible from outside.static
because if not you will need to haveCupcakeMessage
to instantiate it.
All the same I would recommend doing the View
in a separated class.
Android Layout Issue: Getting the Namespace Right
Try to have something like this in your main activity's xml:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.effectivenavigation.MainActivity.NonSwipeableViewPager"/>
Hope this helps.
Edit: I'm still new user and I am not used yet to how extensive my answers should be. So here is a bit more of an example of what I did:
You can extent Fragment
in your custom class and have an onCreateView
method in it. Also you should have a separate layout xml (in my example named fragment_example
) for this fragment. The method should return a View
object that inflates fragment's layout. Here is an example:
public static class ExampleFragment extends Fragment {
static Button b;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_exmaple, container, false);
b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something
}
});
return rootView;
}
}
And here is the corresponding layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".activity_name">
<Button
android:layout_width="227dp"
android:layout_height="40dp"
android:text="Search"
android:id="@+id/search"
android:layout_gravity="center_horizontal"
android:background="#80ffffff"
android:textColor="#ffff6b6b"/>
</LinearLayout>
You will also need an FragmentPagerAdapter
class to create the fragments:
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
default:
return new ExampleFragment();
}
}
@Override
public int getCount() {
return 1;
}
}
Finaly in your activitys onCreate
method you should have something like that:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the two primary sections
// of the app.
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager, attaching the adapter and setting up a listener for when the
// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
}
Heavily based in this: http://developer.android.com/training/implementing-navigation/lateral.html
I hope this offers more detail and helps you more
can't reference the xml layout in Activity
Pls check your imported R ? If you've imported with android.R then import with your package name.
XML Files are Unable to Reference class Files
The solution to this was, as mentioned in the comments of the post above, clearing cached files through file=>invalidates Caches/Restart.
how to inflate a view class in main.xml
it is , but what i was missing is that in order for it to be inflated, the view must be declared static.
Android databinding on inner class not updating TextView
The problem is in how you're mixing data binding layouts with regular layouts.
If you want to include a data binding layout from a regular layout, you need to find the root view of the layout and call bind()
on it. Maybe something like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker_dashboard);
View bindingRoot = findViewById(R.id.toolbar);
LayoutHeaderBinding binding = LayoutHeaderBinding.bind(bindingRoot);
ProfileResponse.Payload profilePayload = new ProfileResponse.Payload();
profilePayload.setFirstName("Test");
binding.setProfilePayload(profilePayload);
}
However, it is better to make your Activity's layout a data binding layout and you won't have to do that extra work:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="profilePayload"
type="myms.models.ProfileResponse.Payload"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Top header -->
<include layout="@layout/layout_header" app:profilePayload="@{profilePayload}" />
<!-- DrawerLayout -->
</android.support.constraint.ConstraintLayout>
</layout>
And then your binding code is much simpler and less error prone:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityPickerDashboardBinding binding =
DataBindingUtil.setContentView(this, R.layout.activity_picker_dashboard);
ProfileResponse.Payload profilePayload = new ProfileResponse.Payload();
profilePayload.setFirstName("Test");
binding.setProfilePayload(profilePayload);
}
On a different note, I think Android data binding and butterknife have significant overlap in functionality and I would recommend choosing one or the other, but not both.
Related Topics
Android/Webkit Text-Overflow: Ellipsis Not Working
Android Webview Rem Units Scale Way to Large for Boxes
Error Referencing an Inner Class View in Layout/Main.Xml
Android and Setting Width and Height Programmatically in Dp Units
How to Use Accelerometer to Measure Distance for Android Application Development
View Not Attached to Window Manager Crash
Losing Data When Rotate Screen
Disable/Check for Mock Location (Prevent Gps Spoofing)
Edittext Loses Content on Scroll in Listview
Android Calendarview for Showing Events
What Is the Default Unit of Style in React Native
Background-Size:Cover Not Working in Android Native Browser
Permission Denial: This Requires Android.Permission.Interact_Across_Users_Full
Displaying Emoticons in Android
Refreshing Data in Recyclerview and Keeping Its Scroll Position
Was Preferencefragment Intentionally Excluded from the Compatibility Package