如何通过单击片段xml文件上的按钮从片段java文件中打开新的Activity?

时间:2016-08-02 04:50:59

标签: java android android-fragments

我需要通过单击片段xml文件上的按钮来打开一个新活动。

我在片段java文件中编码了一下,通过单击片段xml文件上的按钮调用新活动

当我运行应用程序时,它会崩溃

我得到的错误是在行

中的片段java文件中

“public void electronics(查看视图)”

08-02 10:58:55.654 3519-3519/com.example.mohammadzakriya.tabhost E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mohammadzakriya.tabhost, PID: 3519
    java.lang.IllegalStateException: Could not find method electronics(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button2'
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".FolderScale">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="1"
        android:textAlignment="center" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Electronics"
        android:onClick="electronics"/>

</LinearLayout>

爪哇

public class TopfreeFragment extends Fragment {

    public TopfreeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_topfree, container, false);
    }

    public void electronics(View view) {
        Intent intent = new Intent(getActivity(),Electronicspage.class);
        startActivity(intent);
    }
}

3 个答案:

答案 0 :(得分:2)

您在electronics内使用onClick,方法electronic不是electronics

使用electronics代替electronic

答案 1 :(得分:1)

根据我的代码

,最后得到了一些eidt的答案

感谢@ vrund purohit链接How to handle button clicks using the XML onClick within Fragments

它帮助我最终理解了代码在

之下

&#13;
&#13;
// Fragment java file updated

public class TopfreeFragment extends Fragment implements View.OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_topfree, container, false);

        Button b = (Button) v.findViewById(R.id.button2);
        b.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:
                Intent intent = new Intent(getActivity(),Electronicspage.class);
                startActivity(intent);
                break;
        }
    }
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

以下是您可以用来处理按钮的点击事件的另一种方法:

public class TopfreeFragment extends Fragment implements View.OnClickListener{

    View view;
    private Button btn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_topfree, container, false);
        btn = (Button) view.findViewById(R.id.button2);
        btn.setOnClickListener(this);
        return view;
    }

    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.button2:
                 startActivity(new Intent(getActivity(),Electronicspage.class));
            break;

            case R.id.XXXXX:
            break;
            // more button......

        }
    }
}

这篇文章对您有用:Android: how to handle button click