-1

I have fragments with list of elements. When you select element and click it new window with picture and details will apear. Something simple like in Android training That works fine.
Now, in other class Activity I have id of window which I want to open an then I got this error:

No view found for id 0x7f050016

External Activity:

value = 3;
ShowElements newElement = new ShowElements();
Bundle args = new Bundle();
args.putInt("ARG_POSITION", value);
newElement.setArguments(args);

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newElement);
transaction.addToBackStack(null);
transaction.commit();

LogCat:

12-09 18:02:30.104: E/AndroidRuntime(29670): FATAL EXCEPTION: main
12-09 18:02:30.104: E/AndroidRuntime(29670): Process: com.urbanforms.main, PID: 29670
12-09 18:02:30.104: E/AndroidRuntime(29670): java.lang.IllegalArgumentException: No view found for id 0x7f050016 (com.urbanforms.main:id/frag) for fragment ShowElements{42379550 #0 id=0x7f050016}
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.os.Handler.handleCallback(Handler.java:733)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.os.Looper.loop(Looper.java:137)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.app.ActivityThread.main(ActivityThread.java:4998)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at java.lang.reflect.Method.invoke(Method.java:515)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at dalvik.system.NativeStart.main(Native Method)
Serafins
  • 1,237
  • 1
  • 17
  • 36

1 Answers1

1

Override onCreateView in HeadlinesFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.list_of_murals), container, false);
// Initialize your views here 
TextView tv = (TextView) v.findViewById(R.id.textView1);
ListView lv = (ListView) v.findViewById(R.id.list_of_murals);
//setadapter to listview
// listview on item click listener
return v;
}

For details check (Example)

http://developer.android.com/guide/components/fragments.html

Edit:

public class MenuActivity extends FragmentActivity implements
OnHeadlineSelectedListener  {

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

if (findViewById(R.id.fragment_container) != null) {
    if (savedInstanceState != null) {
        return;
    }
    SelectElement selectedElement = new SelectElement();
    selectedElement.setArguments(getIntent().getExtras());
    getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, selectedElement).commit();
}
}

@Override
public void onArticleSelected(int position) {
    ShowElements newElement = new ShowElements();
    Bundle args = new Bundle();
    args.putInt(ShowElements.ARG_POSITION, position);
    newElement.setArguments(args);

    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();

    transaction.replace(R.id.fragment_container, newElement);
    transaction.addToBackStack(null);
    transaction.commit();
}
}

activity_menu.xml

<RelativeLayout 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"
tools:context=".MainActivity" >
 <FrameLayout 
     android:layout_below="@+id/textView1"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.iklikla.codetechblog.FrontpageActivity"
tools:ignore="MergeRootFrame" />

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="murals_list"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

SelectFragment

public class SelectElement extends ListFragment {
    String[] streetName; 
    OnHeadlineSelectedListener mCallback;
    interface OnHeadlineSelectedListener
    {
        public void onArticleSelected(int position);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Resources res = getActivity().getResources();
        streetName = res.getStringArray(R.array.item);
        int layout = android.R.layout.simple_list_item_1;

        setListAdapter(new ArrayAdapter<String>(getActivity(), layout,
                streetName));
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " dupa!");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        mCallback.onArticleSelected(position);

        getListView().setItemChecked(position, true);
    }

}

ShowElements

public class ShowElements extends Fragment {

    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;
    String[] authorsNicks;
    TextView autorTextView; 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.my_fragment2, container, false);
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }

    Resources res = getActivity().getResources();

    authorsNicks = res.getStringArray(R.array.planets_array);
    autorTextView = (TextView) v.findViewById(
            R.id.textView1);

    return v;
}

    @Override
    public void onStart() {
        super.onStart();
        Bundle args = getArguments();
        if (args != null) {
            updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            updateArticleView(mCurrentPosition);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }

    public void updateArticleView(int position) {

        autorTextView.setText(authorsNicks[position]);
        mCurrentPosition = position;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return false;

        }
}

my_framgent2.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp"
        android:text="TextView" />

</RelativeLayout>

string arrays used

 <string-array name="item">
        <item>Austria</item>
        <item>Balraus</item>
        <item>Belgium</item>
    </string-array>
     <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>

Snap

enter image description here enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I updated my onCreateView - look almost like your. I'm trying to replace it – Serafins Dec 09 '13 at 17:36
  • @Serafins you have listview in Headlinesfragment so where is your adapter code and where to you set adapter for listview?? `list_of_murals` which has listview in Headlinesfragment. i guess there is lot of confusion. Pls check the docs again – Raghunandan Dec 09 '13 at 17:44
  • I updated `ShowElement`, because in `HeadlinesFragment` I can not create `onCreateView` - this is `FragmentActivity`. List view I have in Select item. – Serafins Dec 09 '13 at 17:50
  • @Serafins first Headlineframgent is not a fragment?? is a activity??. if so you should rename it to headlinesActivity – Raghunandan Dec 09 '13 at 17:51
  • @Serafins confusin because you din't name your class properly – Raghunandan Dec 09 '13 at 17:52
  • @Serafins even so where is listview initialized and where is the adapter code – Raghunandan Dec 09 '13 at 17:53
  • `setListAdapter(new ArrayAdapter(getActivity(), layout, streetName));` in Select item. Sorry for names, they are very confusing – Serafins Dec 09 '13 at 17:55
  • @Serafins why is activity xml having listview. this is very confusing and docs does not name activity as fragment. you have not checked the docs full `public class HeadlinesFragment extends ListFragment {` still very confusing. I suggest you read the docs full – Raghunandan Dec 09 '13 at 17:56
  • My fault. I will try to change name of classes for correctly if that will help you. And later for sure I will do this same for myself – Serafins Dec 09 '13 at 18:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42804/discussion-between-raghunandan-and-serafins) – Raghunandan Dec 09 '13 at 18:06