【问题标题】:fragment button片段按钮
【发布时间】:2016-02-16 17:43:48
【问题描述】:

我尝试制作一个按钮活动片段,从而使用该片段的 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:

    <?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>

编辑

我删除了 openjdk 并安装了干净的 javasdk 并且工作正常

【问题讨论】:

  • “出了点问题”是什么意思?究竟是什么问题?运行应用时会发生什么?
  • 什么也没发生。按钮不起作用。如果你按下它,什么都不会发生。
  • 发现布局有问题,不小心加了一个封面,占满整个页面,无法给按钮

标签: java android android-layout android-fragments android-fragmentactivity


【解决方案1】:

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

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

【讨论】:

  • 那么我必须在上下文片段java中添加?但如果我在这里添加会出现另一个错误。
  • 我不确定我是否明白你在问什么。也许您可以找到一个如何修改在片段内膨胀的视图的示例?这应该很容易找到。
  • 非常感谢,但我不明白如果我只是添加 ContentFragment.java,这是片段而不是 appactivity 并且不起作用。
  • 我删除了 openjdk 并安装了干净的 javasdk 并且工作正常。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2015-08-07
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多