Search code examples
javaandroidandroid-layoutandroid-fragmentsandroid-fragmentactivity

fragment button


I try to make a button active fragment, leading to a new layout with the url of this fragment. But it is not working. Something wrong. Please help

no receive error, but the button does not work.

code fragment

public class ContentFragment extends Fragment{



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.content_fragment, container, false);
    final View button = view.findViewById(R.id.bSendUrl);
    button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText simpleEditText = (EditText) getView().findViewById(R.id.bTextUrl);
                    String strValue = simpleEditText.getText().toString();
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    intent.putExtra("url", strValue);
                    startActivity(intent);
                }
            }
    );

    return view;
}

}

xml:

    <?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.support.design.widget.TextInputLayout
        android:id="@+id/input_TEXT_Url"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_below="@+id/include"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="6dp">

        <EditText
            android:id="@+id/bTextUrl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textUri"
            android:hint="@string/url_edit_text" />
    </android.support.design.widget.TextInputLayout>
    <Button
        android:layout_width="@dimen/latimea_button_send_tv"
        android:layout_height="@dimen/inaltimea_button_send_tv"
        android:text="@string/button_send_android_tv"
        android:id="@+id/bSendUrl"
        style="?android:attr/borderlessButtonStyle"
        android:layout_below="@+id/input_TEXT_Url"
        android:paddingLeft="16dp"
        android:layout_marginLeft="16dp" />

</RelativeLayout>

Edit

I am removed openjdk and installed clean javasdk and is work fine


Solution

  • You are trying to find a reference to a Button before the fragment's view hierarchy is inflated. The fragment transaction you're using does not immediately create the view hierarchy after commit() is called. This means the view with id bTextUrl is not yet created and findViewById() returns null.

    Also, it's kind of strange for an activity to be messing with the views of child fragments. I recommend that you modify the views of the fragment within the fragment itself (not within the containing activity like you're doing now).