Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 片段按钮_Java_Android_Android Layout_Android Fragments_Android Fragmentactivity - Fatal编程技术网

Java 片段按钮

Java 片段按钮,java,android,android-layout,android-fragments,android-fragmentactivity,Java,Android,Android Layout,Android Fragments,Android Fragmentactivity,我尝试创建一个按钮活动片段,并使用该片段的url创建一个新布局。但它不起作用。有点不对劲。请帮忙 没有接收错误,但按钮不工作 代码片段 public class ContentFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view =

我尝试创建一个按钮活动片段,并使用该片段的url创建一个新布局。但它不起作用。有点不对劲。请帮忙

没有接收错误,但按钮不工作

代码片段

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:


编辑


我被删除了openjdk,安装了干净的javasdk,工作正常

您试图在片段的视图层次结构膨胀之前找到按钮的引用。调用commit()后,您正在使用的片段事务不会立即创建视图层次结构。这意味着id为bTextUrl的视图尚未创建,findViewById()返回null


另外,一个活动会干扰子片段的视图,这有点奇怪。我建议您在片段本身中修改片段的视图(而不是像现在这样在包含活动中)。

那么我必须在上下文片段java中添加吗?但是如果我在这里加上一个错误,我不确定我是否理解你的要求。也许您可以找到一个如何修改片段中膨胀的视图的示例?这应该很容易找到。非常感谢,但我不明白如果我只是添加ContentFragment.java,这是fragment not appactivity,不工作。我被删除了openjdk并安装了干净的javasdk,工作正常。非常感谢你的帮助。“有什么不对劲”是什么意思?到底怎么了?运行应用程序时会发生什么?什么都不会发生。按钮不工作。如果你按下它,什么也不会发生。我发现布局有问题,我不小心添加了一个封面,它占据了整个页面,所以不可能给出按钮
    <?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>