Inflating Layouts in Android

android inflate layout programmatically
android-inflate layout from xml
inflate layout dynamically android
how to remove inflated layout in android
attachtoroot android
inflate view in layout android
layoutinflater in adapter
view.inflate vs layoutinflater

I am trying to make an App that uses a RecyclerView to show a list of Items which are obtained from an Item Array. Everything seems to be going fine, except when I try to run the app on my phone (Lenovo K5 Note, Android 6.0) it crashes instantly. the error that I get is:

Both errors are in the Adapter class and I've marked them.

08-15 21:11:37.771 3163-3163/com.example.mayankthakur.reminderapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mayankthakur.reminderapp, PID: 3163
android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1552)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:3020)
    at android.content.res.Resources.getLayout(Resources.java:1330)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
    at com.example.mayankthakur.reminderapp.MyAdapter.onCreateViewHolder(MyAdapter.java:36)
    at com.example.mayankthakur.reminderapp.MyAdapter.onCreateViewHolder(MyAdapter.java:10)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
    at android.view.View.layout(View.java:16967)
    at android.view.ViewGroup.layout(ViewGroup.java:5584)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1189)
    at android.view.View.layout(View.java:16967)
    at android.view.ViewGroup.layout(ViewGroup.java:5584)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    at android.view.View.layout(View.java:16967)
    at android.view.ViewGroup.layout(ViewGroup.java:5584)
    at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
    at android.view.View.layout(View.java:16967)
    at android.view.ViewGroup.layout(ViewGroup.java:5584)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    at android.view.View.layout(View.java:16967)
    at android.view.ViewGroup.layout(ViewGroup.java:5584)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
    at android.view.View.layout(View.java:16967)
    at android.view.ViewGroup.layout(ViewGroup.java:5584)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2769)
    at android.view.View.layout(View.java:16967)
    at android.view.ViewGroup.layout(ViewGroup.java:5584)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2547)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2250)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1321)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6732)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
    at android.view.Choreographer.doCallbacks(Choreographer.java:696)
    at android.view.Choreographer.doFrame(Choreographer.java:631)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5740)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCall

The Item Class:

public class Item extends AppCompatActivity implements View.OnClickListener{

public Item[] data;

EditText tName;
EditText difficulty;
Button addResource;

private String topicName;
private int time;
private int adifficulty;
private int size;



@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_resource);

    size = data.length;
    tName = findViewById(R.id.editText);
    difficulty = findViewById(R.id.editText2);
    addResource = findViewById(R.id.button);
    addResource.setOnClickListener(this);


    MainActivity.method(data);

   }

public Item (String name, int difficulty){
    this.topicName = name;
    this.adifficulty = difficulty;

}

public String getTopicName() {
    return topicName;
}

public int getTime(){
    return time;
}

public int getDifficulty(){
    return adifficulty;
}

@Override
public void onClick(View view) {
    topicName = tName.getText().toString();
    String i = difficulty.getText().toString();
    adifficulty = Integer.parseInt(i);
    Item thing = new Item (topicName, adifficulty);
    data[size + 1 ] = thing;



}

}

The MainActivity:

 public class MainActivity extends AppCompatActivity implements View.OnClickListener{

static Item thing = new Item("hello", 34);1 // assigning a random value to the dataset to make sure i dont get any nullpointerexceptions

static Item[] theDataset = {thing};
public static void method (Item[] data){
    theDataset = data;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FloatingActionButton nBut = (FloatingActionButton) findViewById(R.id.fab);
    nBut.setOnClickListener(this);

    RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);

    mRecyclerView.setHasFixedSize(true);

    LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    MyAdapter mAdapter = new MyAdapter(theDataset);
    mRecyclerView.setAdapter(mAdapter);

}

@Override
public void onClick(View view) {

    switch (view.getId()) {

        case com.example.mayankthakur.reminderapp.R.id.fab:
            Intent i = new Intent(this, Item.class );
            startActivity(i);

            break;

        default:
            break;
    }


}

}

The AdapterClass:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { //ERROR
private Item [] mDataset;


public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView mTextView;

    public ViewHolder(final View v) {
        super(v);
        mTextView = (TextView) v.findViewById(R.id.textView);
    }
}

// Provide a reference to th    e views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(Item[] myDataset) {
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false); //ERROR
    return new ViewHolder(view);
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    holder.mTextView.setText(mDataset[position].getTopicName());

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.length;
}

}

Any help regarding the issue would be greatly appreciated!

The issue is with viewType it should be your layout xml

LayoutInflater.from(parent.getContext()).inflate(R.layout.your_item.xml, parent, false); 

Understanding Android's LayoutInflater.inflate(), Imagine we specified a button in an XML layout file with its layout width and layout height set to match_parent . <Button xmlns:android="http://  The layout file does not have a root ViewGroup in this example, so we specify our custom LinearLayout to be its root. If our layout file had a FrameLayout as its root instead of <merge>, the FrameLayout and its children would inflate as normal. Then the FrameLayout and children would get added to the LinearLayout, leaving the LinearLayout as the root ViewGroup containing the FrameLayout and children.

Try this:

LayoutInflater.from(parent.getContext()).inflate(R.layout.your_item.xml, parent, false);

LayoutInflater, View child = View.inflate(context, R.layout.child, null) item. <Button xmlns:​android="http://schemas.android.com/apk/res/android"  Android Platform. API level 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1. Manifest.permission. Manifest.permission_group. android.accessibilityservice. AccessibilityService.MagnificationController.OnMagnificationChangedListener. AccessibilityService.SoftKeyboardController.OnShowModeChangedListener.

The Exception you are seeing is caused by this line

final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);

Here, you pass in the parameter viewType from onCreateViewHolder(). Because you did not override getItemViewType() in your RecyclerView.Adapter, it will always be 0.

The default implementation of this method returns 0, making the assumption of a single view type for the adapter.

(Quoted from the documentation on RecyclerView.Adapter)

But inflate() expects a valid layout id at this point, something like R.layout.list_row where list_row.xml can be found in the res/layout folder. 0 is never a valid resource identifier, so your app crashed

Note: You only need to evaluate viewType when you need to inflate different layouts depending on the data for the current position (the runtime will call getItemViewType(int position) to determine the correct value)

How to inflate one view with a layout, In Android, you can create an UI: decoratively through a layout XML. with Java object view (viewgroup for container and views for widget). In this context, Inflate​  Helper class for inflating layouts asynchronously. To use, construct an instance of AsyncLayoutInflater on the UI thread and call inflate(int, ViewGroup, OnInflateFinishedListener). The AsyncLayoutInflater.OnInflateFinishedListener will be invoked on the UI thread when the inflate request has completed. This is

Android - Inflate (a layout XML to a view UI object) [Gerardnico, to remember when working with LayoutInflater's 'Inflate' method on Android. The second argument, rootView is used to calculate the layout  Inflating and Encapsulating Layout XML into View Object in Android. There are two different ways that layout files in xml can be inflated into the Activity class. The most straightforward method, usually seen in introductory materials demonstrating android Activity, is to use the the xml in setContentView function for the activity.

android.view.LayoutInflater.inflate java code examples, Inflating different . layoutChildren(FrameLayout.java:573) at android.widget. ViewGroup.layout(ViewGroup.java:4969) at android.widget. Generally, we develop the layout for an Android application by creating the XML file. These are called static layouts. Yes, because you can not add/delete any View Type in XML on runtime. Dynamic

Inflating a FrameLayout - [Android Classifieds App], Inflate Layout at Runtime in Kotlin Android Asad Ali Choudhry April 9, 2020 April 9, 2020 In this article, you will learn how to inflate one layout in another at Runtime using Kotlin in Android. To populate very few items, we can embed or inflate them on runtime.

Comments
  • Possible duplicate of Android - android.content.res.Resources$NotFoundException: Resource ID #0x0
  • your data is null and what is this MainActivity.method(data);?