从扩展Fragment

时间:2017-10-23 11:05:36

标签: java android android-fragments

我在这里经历过很多关于从片段开始活动的帖子,尽管它们似乎都没有解决我的问题。正如我所说,我试图从扩展片段的类开始一个活动。在那一刻,当我删除" android:onClick"从xml,按钮只是变得死了,它不让我点击它。但是,当我将OnClick方法放入xml时,我收到以下错误:

  

java.lang.IllegalStateException:无法在父级或祖先中找到方法onClick(View)用于android的onClick属性:在视图类android.support.v7.widget.AppCompatButton上定义的带有id' buttonViewDetails&#39的onClick属性;                                                                          在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)                                                                          在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)                                                                          在android.view.View.performClick(View.java:5653)                                                                          在android.view.View $ PerformClick.run(View.java:22509)                                                                          在android.os.Handler.handleCallback(Handler.java:751)                                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                                          在android.os.Looper.loop(Looper.java:154)                                                                          在android.app.ActivityThread.main(ActivityThread.java:6144)                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我不太清楚这意味着什么。这是我尝试过的代码:

类扩展片段中的按钮XML:

 <Button
    android:id="@+id/buttonViewDetails"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@mipmap/info"
    android:layout_marginLeft="250dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="14dp"
    android:layout_marginBottom="14dp" />

扩展fragment的类的Java代码:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myInflatedView = inflater.inflate(R.layout.line_details, container, false);

    Bundle extras = getArguments();
    if (extras != null) {
        String linetype = extras.getString("LineType");
        TextView LT = (TextView) getActivity().findViewById(R.id.textViewLineType);
        LT.setText(linetype);

        Button InfoButton = (Button) getActivity().findViewById(R.id.buttonViewDetails);
        InfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), VerifyLine.class);
                startActivity(intent);
            }
        });

    }

    return myInflatedView;
}

有人能帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在使用活动参考对视图myInflatedView进行充气并对该视图执行findViewById。因此,请更改该getActivity并使用myInflatedView查找Views.Also从xml中删除onClick for the button。

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View myInflatedView = inflater.inflate(R.layout.line_details, container, false);

        TextView LT = (TextView) myInflatedView.findViewById(R.id.textViewLineType);
        Button InfoButton = (Button) myInflatedView.findViewById(R.id.buttonViewDetails);
        InfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), VerifyLine.class);
                startActivity(intent);
            }
        });

        Bundle extras = getArguments();
        if (extras != null) {
            String linetype = extras.getString("LineType");
            LT.setText(linetype);
        }

        return myInflatedView;
    }