P
P
pitimirov_a2015-11-14 23:54:25
Android
pitimirov_a, 2015-11-14 23:54:25

Can't catch elements with fragment layout in java. Why?

In activity main

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" />

In fragment_main.xml
This is the file whose elements are displayed on the screen...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/fragment"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment"
    android:padding="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:layout_gravity="center_horizontal"
        android:background="@color/main">...

java core
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        connectWebSocket();

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }

...

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);


            return rootView;
        }
    }

and when adding a sikbar (to any fragment of this code), the application is interrupted and the log returns an access error Activity...
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
belozerow, 2015-11-15
@belozerow

You can, of course, search through activities, but it is advisable to use
rootView.findViewById(R.id.seekBar);
Accordingly, the rootView is available to you only after its creation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question