Android - No view found for fragment

2.3k views Asked by At

I'm trying to get my replaced fragment to appear in the detail fragment but after debugging my app on a tablet emulator and selecting the necessary list view item, the log cat gives me this error:

Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c0050 (com.apptacularapps.exitsexpertlondonlite:id/detail_container) for fragment FragmentWCBank{3b4ea8fb #0 id=0x7f0c0050}

I really don't understand why I'm getting this error when my fragment is there within my project. Does anyone know what I'm doing wrong and how to fix this?

FragmentWCLine.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentWCLine extends android.support.v4.app.Fragment {

    public final static String EXTRA_MESSAGE = "Station_key";

    private class WC {
        private CharSequence station;
        private CharSequence zone;
        private Class<? extends Activity> activityClass;
        private Class<? extends android.support.v4.app.Fragment> fragmentClass;

        public WC(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
            this.fragmentClass = fragmentClass;
            this.activityClass = activityClass;
            this.station = getResources().getString(stationResId);
            this.zone = getResources().getString(zoneResId);
        }

        @Override
        public String toString() { return station.toString(); }
        public String getzone(){ return zone.toString(); }
    }

    private static WC[] mWC;

    private boolean mTwoPane;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_wc_line, container, false);

        // Instantiate the list of stations.
        mWC = new WC[]{
                new WC(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
                new WC(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
        };

        final ListView listView = (ListView)v.findViewById(R.id.list_wc);
        listView.setAdapter(new MyAdapter(getActivity(), mWC));
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mTwoPane){

                    setItemNormal();
                    View rowView = view;
                    setItemSelected(rowView);
                }
                else{
                    Intent intent = new Intent(getActivity(), mWC[position].activityClass);
                    String station = mWC[position].station.toString();
                    intent.putExtra(EXTRA_MESSAGE, station);

                    startActivity(intent);
                }
            }

            public void setItemSelected(View view){
                View rowView = view;
                view.setBackgroundColor(Color.parseColor("#66CCCC"));

                TextView tv0 = (TextView)rowView.findViewById(R.id.list_item_station);
                tv0.setTextColor(Color.parseColor("#000099"));

                TextView tv1 = (TextView)rowView.findViewById(R.id.list_item_zone);
                tv1.setTextColor(Color.parseColor("#000099"));
            }

            public void setItemNormal()
            {
                for (int i=0; i< listView.getChildCount(); i++) {
                    View v = listView.getChildAt(i);
                    v.setBackgroundColor(Color.TRANSPARENT);

                    TextView tv0 = ((TextView) v.findViewById(R.id.list_item_station));
                    tv0.setTextColor(Color.WHITE);

                    TextView tv1 = ((TextView) v.findViewById(R.id.list_item_zone));
                    tv1.setTextColor(Color.parseColor("#B5B5B5"));
                }
            }
        });

        return v;
    }

    static class MyAdapter extends BaseAdapter {

        static class ViewHolder {
            TextView station;
            TextView zone;
        }

        LayoutInflater inflater;
        WC[] mWC;

        public MyAdapter(Context contexts, WC[] samples) {
            this.mWC = samples;
            inflater = LayoutInflater.from(contexts);
        }

        @Override
        public int getCount() {
            return mWC.length;
        }

        @Override
        public Object getItem(int position) {
            return mWC[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        /**set selected position**/
        private int selectPosition = -1;
        public void setSelectPosition(int position){
            if(position!=selectPosition){
                selectPosition = position;
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_item_dualline, null);
                viewHolder = new ViewHolder();

                viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
                viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.station.setText(mWC[position].station);
            viewHolder.zone.setText(mWC[position].getzone());

            //change item color
            if(position==selectPosition){
                convertView.setBackgroundColor(Color.parseColor("#000099"));
                viewHolder.station.setTextColor(Color.parseColor("#000099"));
            }else {

            }

            return convertView;
        }
    }
}

WCBankActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;


public class WCBankActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "Station_key";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_wc_bank);

        if (savedInstanceState == null) {
            // Get the message from the intent
            Intent intent = getIntent();
            // Notice to specify the sender Activity for the message
            String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);

            FragmentWCBank newFragment = new FragmentWCBank();
            FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.detail_container, newFragment);
            transaction.commit();
        }
    }
}

FragmentWCBank.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentWCBank extends android.support.v4.app.Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_wc_bank, container, false);

        return v;
    }
}

fragment inflation enter image description here enter image description here

fragment_wc_bank.xml layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/detail_container">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Bank"
        android:id="@+id/textView0"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

fragment_wc_line.xml layout

<?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"
    android:id="@+id/fragmentwcline">

    <ListView
        android:id="@+id/list_wc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="false"
        android:layout_centerHorizontal="true"/>

</LinearLayout>

activity_main.xml layout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/master_container"
    android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

activity_main.xml layout (sw600dp)

<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="match_parent"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/master_container"/>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/detail_container"/>

</LinearLayout>

Undesired result enter image description here

2

There are 2 answers

2
The Original Android On BEST ANSWER

Your code transaction.replace(R.id.detail_container...Now this may be the correct UI element. I am assuming this is the correct UI. If so, fragment_wc_bank layout xml does not have that ID detail_container, hence you get error, simple as that. However activity_main.xml has that ID, again I am assuming detail_container is the correct UI that you want.

Suggested code FROM:

setContentView(R.layout.fragment_wc_bank);

TO:

setContentView(R.layout.activity_main.xml);

Notes:

  1. activity_main.xml is the only difference between the 2 codes above.
  2. This is a common error but we all have to learn from this. Learn more about layouts. It is a critical knowledge in making Android apps.
  3. There are 2 different activity_main.xml files you posted. They should at least be similar. Otherwise you could be very confused.
2
Gabe Sechan On

Your activity is trying to inflate a layout, then place a fragment into it in place of the view detail_container. detail_container does not exist in that layout- either you forgot to add it or you're inflating the wrong thing. I notice you're inflating R.layout.fragment_wc_bank for both the fragment and the main activity, my guess is you're inflating the wrong layout in the activity.